-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20. Valid Parentheses.py
More file actions
43 lines (34 loc) · 873 Bytes
/
Copy path20. Valid Parentheses.py
File metadata and controls
43 lines (34 loc) · 873 Bytes
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
41
42
43
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
dict_parentesis = {"}":"{", ")":"(",">":"<","]":"[" }
stack = []
for char in s:
if len(stack) == 0:
stack.append(char)
else:
# temp =
#print()
if char in dict_parentesis:
if dict_parentesis[char] == stack[-1]:
stack.pop()
else:
stack.append(char)
else:
stack.append(char)
if len(stack) == 0:
return True
else:
return False
#return True
sol = Solution()
z_list = ["()","()[]{}","(]","([)]","{[]}"]
for z in z_list:
print(sol.isValid(z))
'''
Time - O(n)
Space - O(n)
'''