-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_valid_parentheses.py
More file actions
30 lines (28 loc) · 2.37 KB
/
Copy path20_valid_parentheses.py
File metadata and controls
30 lines (28 loc) · 2.37 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
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s) % 2 != 0:
return False
twins = { '}': '{', ']': '[', ')' : '('}
expression = []
for ch in s:
if len(expression) > 0 and (ch in twins and expression[-1] == twins[ch]):
expression.pop()
else:
expression.append(ch)
return True if len(expression) == 0 else False
vectors = [
"((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}((([][][][{{{}}}]{{}}([])))){}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", True,
'){', False,
'(){}}{', False,
'[([]])', False
]
for i in range(0, len(vectors), 2):
s = vectors[i]
expected = vectors[i + 1]
print(f'{s} {expected}')
returned = Solution().isValid(s)
assert expected == returned, f'for {s} expected {expected}, returned {returned}!'