-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidParentheses.py
31 lines (30 loc) · 1003 Bytes
/
ValidParentheses.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
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for i in range(len(s)):
if(s[i] == "(" or s[i] == "{" or s[i] == "["):
stack.append(s[i])
else:
if(stack):
temp = stack[-1]
if(s[i] == ")"):
if(temp == "("):
stack.pop()
else:
return False
elif(s[i] == "}"):
if(temp == "{"):
stack.pop()
else:
return False
else:
if(temp == "["):
stack.pop()
else:
return False
else:
return False
if(len(stack) == 0):
return True
else:
return False