-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_parentheses
More file actions
30 lines (27 loc) · 857 Bytes
/
valid_parentheses
File metadata and controls
30 lines (27 loc) · 857 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
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
n = len(s)
if n == 0: return True
if n%2 != 0: return False
while '()' in s or '[]' in s or '{}' in s:
s = s.replace('{}','').replace('()','').replace('[]','')
if len(s) == 0: return True
else: return False
class Solution2:
# @return a boolean
def isValid(self, s):
stack = []
dict = {"]":"[", "}":"{", ")":"("}
for char in s:
if char in dict.values():
stack.append(char)
elif char in dict.keys():
if stack == [] or dict[char] != stack.pop():
return False
else:
return False
return stack == []