forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
46 lines (45 loc) · 1 KB
/
Solution.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
a="()"
b="[]"
c="{}"
f=False
if s == '':
return True
while s != '':
s=s.replace(a,'')
s=s.replace(b,'')
s=s.replace(c,'')
if s == '':
f=True
return f
if (a in s) or (b in s) or (c in s):
pass
else:
f=False
return f
class Solution():
def isValid(self,s):
"""
:type s: str
:rtype: bool
"""
left=['(','{','[']
right={ ')':'(',
']':'[',
'}':'{' }
stack=[]
for i in s:
if i in left:
stack.append(i)
elif stack and right[i] == stack.pop():
continue
else:
return False
if stack:
return False
return True