We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2e1aea0 commit f21d700Copy full SHA for f21d700
Python/parsing-a-boolean-expression.py
@@ -0,0 +1,30 @@
1
+# Time: O(n)
2
+# Space: O(n)
3
+
4
+class Solution(object):
5
+ def parseBoolExpr(self, expression):
6
+ """
7
+ :type expression: str
8
+ :rtype: bool
9
10
+ def parse(expression, i):
11
+ if expression[i[0]] not in "&|!":
12
+ result = expression[i[0]] == 't'
13
+ i[0] += 1
14
+ return result
15
+ op = expression[i[0]]
16
+ i[0] += 2
17
+ stk = []
18
+ while expression[i[0]] != ')':
19
+ if expression[i[0]] == ',':
20
21
+ continue
22
+ stk.append(parse(expression, i))
23
24
+ if op == '&':
25
+ return all(stk)
26
+ if op == '|':
27
+ return any(stk)
28
+ return not stk[0]
29
30
+ return parse(expression, [0])
0 commit comments