-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1106. Parsing A Boolean Expression.cpp
42 lines (41 loc) · 1.13 KB
/
1106. Parsing A Boolean Expression.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
class Solution {
public:
bool parseBoolExpr(string expression)
{
stack<char>st;
for(auto ch:expression)
{
if(ch == ')')
{
char flag = 't';
unordered_set<char>ust;
while(st.top() != '(')
{
if(ust.size() <= 2)
ust.insert(st.top());
st.pop();
}
st.pop(); // poped '('
if(st.top() == '&')
{
if(ust.count('f'))
flag = 'f';
}
else if(st.top() == '|')
{
if(not ust.count('t'))
flag = 'f';
}
else if(st.top() == '!')
{
if(ust.count('t'))
flag = 'f';
}
st.pop(); // poped '&' or '|' or '!'
st.push(flag); // flag = 't' or 'f'
}
else st.push(ch);
}
return (st.top() == 't' ? true : false);
}
};