codekofi
← All problems

Problem 65

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1bool solution(const string& s) {
2
3 int low = 0;
4 int high = 0;
5
6 for (char c : s) {
7
8 if (c == '(') {
9 low++;
10 high++;
11
12 } else if (c == ')') {
13 low--;
14 high--;
15
16 } else {
17 low--;
18 high++;
19 }
20
21 if (high < 0) {
22 return false;
23 }
24
25 if (low < 0) {
26 low = 0;
27 }
28 }
29
30 return low == 0;
31}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.