-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0020.py
More file actions
56 lines (43 loc) · 1.51 KB
/
Copy path0020.py
File metadata and controls
56 lines (43 loc) · 1.51 KB
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
47
48
49
50
51
52
53
54
55
56
"""
Given a string s containing just the characters
'(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
"""
class Solution:
def isValid(self, s: str) -> bool:
open_chars = ['(', '{', '[']
closing_chars = [')', '}', ']']
opening_chars = set(open_chars)
matching_chars = dict(zip(closing_chars, open_chars))
char_open = []
for char in s:
# Always accept open chars
if char in opening_chars:
char_open.append(char)
continue
# it was a closing char, does it match?
try:
if char_open[-1] != matching_chars[char]:
#print(f"Error: closing bracket mismatch on {char}")
return False
except IndexError:
#print("Error: there were no open brackets")
return False
# it was a match
char_open.pop()
# If we have open brackets left return false
if char_open:
#print("Error: open chars left")
return False
# Everything seems okay
return True
if __name__ == "__main__":
c = Solution()
# testcases
print(c.isValid("()"))
print(c.isValid("()[]{}"))
print(c.isValid("(]"))
print(c.isValid("([)]"))
print(c.isValid("{[]}"))