-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20-IsValid.py
More file actions
31 lines (29 loc) · 901 Bytes
/
20-IsValid.py
File metadata and controls
31 lines (29 loc) · 901 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:
def isValid(self, s: str) -> bool:
stk = list()
for c in s:
if c == '(' or c == '{' or c == '[':
stk.append(c)
else:
if stk:
if c == ')' and stk[-1] == '(':
stk.pop()
elif c == '}' and stk[-1] == '{':
stk.pop()
elif c == ']' and stk[-1] == '[':
stk.pop()
else:
return False
else:
return False
if stk:
return False
else:
return True
class Solution:
def isValid(self, s):
while '{}' in s or '()' in s or '[]' in s:
s = s.replace('{}', '')
s = s.replace('[]', '')
s = s.replace('()', '')
return s == ''