-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParenthesis Checker.py
33 lines (29 loc) · 1.2 KB
/
Parenthesis Checker.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
33
"""
https://practice.geeksforgeeks.org/problems/parenthesis-checker/0
Given an expression string exp. Examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.
For example, the program should print 'balanced' for exp = “[()]{}{[()()]()}” and 'not balanced' for exp = “[(])”
"""
__author__ = 'abhireddy96'
class Solution:
def isValid(self, s: str) -> bool:
# Hash map of closing and opening parenthesis
d = {')': '(', ']': '[', '}': '{'}
ls = []
# Iterate over string
for i in s:
# Check if it is closing parenthesis
if i in d:
# If so, pop top element from stack
sym = ls.pop() if ls else ''
# Compare if parenthesis of same type
# If not immediately return False
if d[i] != sym:
return 'not balanced'
# Check if it is opening parenthesis
# Add it to stack
else:
ls.append(i)
return 'balanced' if ls == [] else 'not balanced'
if __name__ == "__main__":
for _ in range(int(input())):
print(Solution().isValid(input()))