1
class Solution {
2
public:
3
bool parseBoolExpr(string e) {
4
auto it = e.begin();
5
return parse(it);
6
}
7

8
bool parse(string::iterator &it) {
9
switch (*(it++)) {
10
case 't':
11
return true;
12
case 'f':
13
return false;
14
case '|':
15
return parse(it, false, [](bool init, bool val) { return init || val; });
16
case '&':
17
return parse(it, true, [](bool init, bool val) { return init && val; });
18
case '!':
19
return parse(it, false, [](bool init, bool val) { return !val; });
20
}
21
return false;
22
}
23

24
bool parse(string::iterator &it, bool init, function<bool(bool, bool)> op) {
25
it++; // skip '('
26
while (true) {
27
auto b = parse(it);
28
init = op(init, b);
29
if (*it == ')') break;
30
it++; // skip ','
31
}
32
it++; // skip ')'
33
return init;
34
}
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0