codekofi
← All problems

Problem 5

Easy30 XP

1 · Worked examples

0 / 4

Give it an input and say what you think comes back. It compiles and runs for real, so a wrong guess still shows you the answer — probe as much as you like. 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 vector<char> pending;
3 for (char c : s) {
4 if (c == '(' || c == '[' || c == '{') {
5 pending.push_back(c);
6 continue;
7 }
8 if (pending.empty()) return false;
9 char top = pending.back();
10 pending.pop_back();
11 if (c == ')' && top != '(') return false;
12 if (c == ']' && top != '[') return false;
13 if (c == '}' && top != '{') return false;
14 }
15 return pending.empty();
16}

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.