-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.py
32 lines (29 loc) · 1.04 KB
/
solution.py
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
class Solution:
def parseBoolExpr(self, expression: str) -> bool:
stack = []
for c in expression:
if c == ')':
sub_expr = []
while stack[-1] != '(':
sub_expr.append(stack.pop())
stack.pop() # remove '('
op = stack.pop() # get the operator
if op == '!':
stack.append('f' if sub_expr[0] == 't' else 't')
elif op == '&':
result = 't'
for e in sub_expr:
if e == 'f':
result = 'f'
break
stack.append(result)
elif op == '|':
result = 'f'
for e in sub_expr:
if e == 't':
result = 't'
break
stack.append(result)
elif c != ',':
stack.append(c)
return stack[0] == 't'