-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.py
More file actions
48 lines (36 loc) · 1.13 KB
/
20.py
File metadata and controls
48 lines (36 loc) · 1.13 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
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s) % 2 != 0:
return False
reference_map = {
'(' : ')',
'{' : '}',
'[' : ']',
}
# half_len = len(s) // 2
closing_parantheses_stack = []
for i in range(0, len(s)):
if s[i] in reference_map:
closing_parantheses_stack.append(reference_map[s[i]])
else:
if len(closing_parantheses_stack) == 0 or s[i] != closing_parantheses_stack.pop():
return False
# return
# for i in range(0, half_len):
# if s[i] not in reference_map:
# print(s[i])
# return False
# else:
# closing_parantheses_stack.append(reference_map[s[i]])
# for i in range(half_len, len(s)):
# if closing_parantheses_stack.pop() != s[i]:
# return False
return True
if __name__ == '__main__':
s = Solution()
# print(s.isValid('()'))
print(s.isValid("()[]{}"))