-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_valid_parenthesis.py
More file actions
31 lines (31 loc) · 854 Bytes
/
Copy path20_valid_parenthesis.py
File metadata and controls
31 lines (31 loc) · 854 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
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
opened = []
L = len(s)
for i in range(L):
char = s[i]
if char in ['(', '[', '{']:
opened.append(char)
else:
try:
last = opened.pop()
except:
return(False)
if last == '(':
if char in [']', '}']:
return(False)
elif last == '[':
if char in [')', '}']:
return(False)
else:
if char in [')', ']']:
return(False)
try:
opened.pop()
return(False)
except:
return(True)