-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path020 Valid Parentheses.py
36 lines (32 loc) · 1.04 KB
/
020 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
'''
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
'''
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
# Valid str must be even
if len(s) % 2 == 1:
return False
stack = []
left = ("(", "[", "{")
right = (")", "]", "}")
zip(left,right)
for v in s:
if v in left:
stack.append(v)
else:
if not stack:
return False
p = stack.pop()
if left.index(p) != right.index(v):
return False
return len(stack) == 0
if __name__ == "__main__":
assert Solution().isValid("({}){}") == True
assert Solution().isValid("({)}") == False
assert Solution().isValid("}}}") == False
assert Solution().isValid("(((") == False