-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.py
25 lines (22 loc) · 798 Bytes
/
answer.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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# solution
#-------------------------------------------------------------------------------
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for i in s:
if i == '(' or i == '[' or i == '{':
stack.append(i)
elif i == ')' and stack and stack[-1] == '(' or \
i == ']' and stack and stack[-1] == '[' or \
i == '}' and stack and stack[-1] == '{':
stack.pop()
else:
return False
return len(stack) == 0
#-------------------------------------------------------------------------------