Skip to content

Commit 08b438a

Browse files
committedApr 4, 2025
Add solution #1106
1 parent 6430330 commit 08b438a

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,137 LeetCode solutions in JavaScript
1+
# 1,138 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -880,6 +880,7 @@
880880
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
881881
1104|[Path In Zigzag Labelled Binary Tree](./solutions/1104-path-in-zigzag-labelled-binary-tree.js)|Medium|
882882
1105|[Filling Bookcase Shelves](./solutions/1105-filling-bookcase-shelves.js)|Medium|
883+
1106|[Parsing A Boolean Expression](./solutions/1106-parsing-a-boolean-expression.js)|Hard|
883884
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|
884885
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|
885886
1123|[Lowest Common Ancestor of Deepest Leaves](./solutions/1123-lowest-common-ancestor-of-deepest-leaves.js)|Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1106. Parsing A Boolean Expression
3+
* https://leetcode.com/problems/parsing-a-boolean-expression/
4+
* Difficulty: Hard
5+
*
6+
* A boolean expression is an expression that evaluates to either true or false. It can be in
7+
* one of the following shapes:
8+
* - 't' that evaluates to true.
9+
* - 'f' that evaluates to false.
10+
* - '!(subExpr)' that evaluates to the logical NOT of the inner expression subExpr.
11+
* - '&(subExpr1, subExpr2, ..., subExprn)' that evaluates to the logical AND of the inner
12+
* expressions subExpr1, subExpr2, ..., subExprn where n >= 1.
13+
* - '|(subExpr1, subExpr2, ..., subExprn)' that evaluates to the logical OR of the inner
14+
* expressions subExpr1, subExpr2, ..., subExprn where n >= 1.
15+
*
16+
* Given a string expression that represents a boolean expression, return the evaluation of
17+
* that expression.
18+
*
19+
* It is guaranteed that the given expression is valid and follows the given rules.
20+
*/
21+
22+
/**
23+
* @param {string} expression
24+
* @return {boolean}
25+
*/
26+
var parseBoolExpr = function(expression) {
27+
const stack = [];
28+
29+
for (const char of expression) {
30+
if (char === ')') {
31+
const operands = [];
32+
while (stack[stack.length - 1] !== '(') {
33+
operands.push(stack.pop());
34+
}
35+
stack.pop();
36+
const operator = stack.pop();
37+
38+
if (operator === '!') {
39+
stack.push(!operands[0]);
40+
} else if (operator === '&') {
41+
stack.push(operands.every(val => val));
42+
} else if (operator === '|') {
43+
stack.push(operands.some(val => val));
44+
}
45+
} else if (char !== ',') {
46+
stack.push(char === 't' ? true : char === 'f' ? false : char);
47+
}
48+
}
49+
50+
return stack[0];
51+
};

0 commit comments

Comments
 (0)
Please sign in to comment.