Skip to content

Commit

Permalink
Oct 20
Browse files Browse the repository at this point in the history
  • Loading branch information
siddydutta committed Oct 20, 2024
1 parent fd75800 commit e22662d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution:
BOOL_MAP = {'t': True, 'f': False}

def parseBoolExpr(self, expression: str) -> bool:
stack = list()
for ch in expression:
if ch == ')':
vals = set()
while stack[-1] != '(':
vals.add(stack.pop())
stack.pop()
operation = stack.pop()
if operation == '&':
stack.append(all(vals))
elif operation == '|':
stack.append(any(vals))
else:
stack.append(not vals.pop())
elif ch != ',':
stack.append(self.BOOL_MAP.get(ch, ch))
return stack.pop()


def main():
expression = '&(|(f))'
assert Solution().parseBoolExpr(expression) is False

expression = '|(f,f,f,t)'
assert Solution().parseBoolExpr(expression) is True

expression = '!(&(f,t))'
assert Solution().parseBoolExpr(expression) is True


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions 2024-10-October-LeetCoding-Challenge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
| October 17 | [670. Maximum Swap](https://leetcode.com/problems/maximum-swap/) | Medium | Solved |
| October 18 | [2044. Count Number of Maximum Bitwise-OR Subsets](https://leetcode.com/problems/count-number-of-maximum-bitwise-or-subsets/) | Medium | Unsolved |
| October 19 | [1545. Find Kth Bit in Nth Binary String](https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/) | Medium | Unsolved |
| October 20 | []() | | |
| October 20 | [1106. Parsing A Boolean Expression](https://leetcode.com/problems/parsing-a-boolean-expression/) | Hard | Solved |
| October 21 | []() | | |
| October 22 | []() | | |
| October 23 | []() | | |
Expand All @@ -41,4 +41,4 @@
| --- | --- | --- | --- |
| Easy | 2 | 2 | 0 |
| Medium | 16 | 8 | 8 |
| Hard | 1 | 0 | 1 |
| Hard | 2 | 1 | 1 |

0 comments on commit e22662d

Please sign in to comment.