-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.js
45 lines (41 loc) · 969 Bytes
/
solution.js
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
/**
* @param {string} expression
* @return {boolean}
*/
var parseBoolExpr = function (expression) {
let stack = [];
for (let c of expression) {
if (c === ")") {
let subExpr = [];
while (stack[stack.length - 1] !== "(") {
subExpr.push(stack.pop());
}
stack.pop(); // Remove '('
let op = stack.pop(); // Get the operator
if (op === "!") {
stack.push(subExpr[0] === "t" ? "f" : "t");
} else if (op === "&") {
let result = "t";
for (let e of subExpr) {
if (e === "f") {
result = "f";
break;
}
}
stack.push(result);
} else if (op === "|") {
let result = "f";
for (let e of subExpr) {
if (e === "t") {
result = "t";
break;
}
}
stack.push(result);
}
} else if (c !== ",") {
stack.push(c);
}
}
return stack[0] === "t";
};