forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
40 lines (37 loc) · 981 Bytes
/
Copy pathmain.py
File metadata and controls
40 lines (37 loc) · 981 Bytes
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
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
pairs = {
')': '(',
'}': '{',
']': '['
}
lefts = pairs.values()
stack = [s[0]]
pivot = 1
while pivot < len(s):
ch = s[pivot]
if ch in lefts:
stack.append(ch)
elif ch in pairs:
if len(stack) == 0 or stack[-1] != pairs[ch]:
return False
else:
stack.pop()
else:
return False
pivot += 1
return len(stack) == 0
import os
if os.getenv('LZS'): # local test
s = Solution()
assert s.isValid('{}{}{}')
assert s.isValid('{}{[()]}{}')
assert s.isValid('(){}(){[()]}{}')
assert not s.isValid('((){[()]}{}')
assert not s.isValid('')
assert not s.isValid('{')
assert not s.isValid('[])')