-
Notifications
You must be signed in to change notification settings - Fork 17
/
valid-parentheses.py
46 lines (36 loc) · 1.18 KB
/
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
41
42
43
44
45
46
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
valid_open_brackets = ["(", "{", "["]
valid_close_brackets = [")", "}", "]"]
close_open_brackets_map = {
")": "(",
"}": "{",
"]": "[",
}
brackets_stack = []
result = True
for symbol in s:
if symbol in valid_open_brackets:
brackets_stack.append(symbol)
elif symbol in valid_close_brackets:
if len(brackets_stack) == 0 or \
close_open_brackets_map[symbol] != brackets_stack.pop():
result = False
break
if len(brackets_stack) > 0:
result = False
return result
solution = Solution()
for i in range(1000000):
assert solution.isValid("") == True
assert solution.isValid("[") == False
assert solution.isValid(")") == False
assert solution.isValid("()") == True
assert solution.isValid("()[]{}") == True
assert solution.isValid("(]") == False
assert solution.isValid("([)]") == False
assert solution.isValid("{[]}") == True