-
Notifications
You must be signed in to change notification settings - Fork 3
/
q0020.py
33 lines (31 loc) · 920 Bytes
/
q0020.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
#!/usr/bin/python3
class Solution:
def isValid(self, s: str) -> bool:
stack = []
dic = {'{': '}', '[': ']', '(': ')'}
for i in s:
if i in dic.keys():
stack.append(dic[i])
elif len(stack) <= 0 or stack.pop() != i:
return False
if len(stack) == 0:
return True
else:
return False
def isValid2(self, s: str) -> bool:
stack = []
for i in s:
if i == '{' or i == '(' or i == '[':
stack.append(i)
elif len(stack) <= 0:
return False
else:
pre = stack.pop()
if not (ord(i) == ord(pre) + 1 or ord(i) == ord(pre) + 2):
return False
if len(stack) == 0:
return True
else:
return False
solu = Solution()
print(solu.isValid("{"))