-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_valid_parentheses.py
40 lines (33 loc) · 1.01 KB
/
20_valid_parentheses.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
34
35
36
37
38
39
40
class Stack:
def __init__(self):
self.stack = []
def isEmpty(self):
return self.size() == 0
def push(self, item):
self.stack.append(item)
def pop(self):
return self.stack.pop()
def peek(self):
return self.stack[self.size() - 1]
def size(self):
return len(self.stack)
class Solution:
def isValid(self, s: str) -> bool:
st = Stack()
opening_braces = ['(', '{', '[']
closing_braces = [')', '}', ']']
for ch in s:
if ch in opening_braces:
st.push(ch)
elif ch in closing_braces:
if st.isEmpty():
return False
top_ch = st.peek()
if (top_ch == '(' and ch == ')') or (top_ch == '{' and ch == '}') or (top_ch == '[' and ch == ']'):
st.pop()
else:
return False
return st.isEmpty()
sol = Solution()
inp = input()
print(sol.isValid(inp))