codekofi
← All problems

Problem 5

Easy

1 · Worked examples

0 / 4

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 4 outputs correctly.

The accepted solution

1bool solution(const string& s) {
2
3 stack<char> st;
4
5 for (char c : s) {
6
7 if (c == '(' || c == '[' || c == '{') {
8 st.push(c);
9 continue;
10 }
11
12 if (st.empty()) {
13 return false;
14 }
15
16 char top = st.top();
17 st.pop();
18
19 if (c == ')' && top != '(') return false;
20 if (c == ']' && top != '[') return false;
21 if (c == '}' && top != '{') return false;
22 }
23
24 return st.empty();
25}

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.