-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
62 lines (58 loc) · 1.55 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution
{
public:
bool parseBoolExpr(string expression)
{
stack<char> st;
for (char c : expression)
{
if (c == ')')
{
vector<char> subExpr;
while (st.top() != '(')
{
subExpr.push_back(st.top());
st.pop();
}
st.pop(); // Remove '('
char op = st.top();
st.pop(); // Remove the operator
if (op == '!')
{
st.push(subExpr[0] == 't' ? 'f' : 't');
}
else if (op == '&')
{
char result = 't';
for (char e : subExpr)
{
if (e == 'f')
{
result = 'f';
break;
}
}
st.push(result);
}
else if (op == '|')
{
char result = 'f';
for (char e : subExpr)
{
if (e == 't')
{
result = 't';
break;
}
}
st.push(result);
}
}
else if (c != ',')
{
st.push(c);
}
}
return st.top() == 't';
}
};