File tree Expand file tree Collapse file tree 1 file changed +12
-15
lines changed Expand file tree Collapse file tree 1 file changed +12
-15
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/python3
2
2
# -*- coding: utf-8 -*-
3
3
'''
4
- AC but not a good solution
4
+ AC
5
5
'''
6
6
import time
7
7
@@ -12,23 +12,18 @@ def isValid(self, s):
12
12
:type s: str
13
13
:rtype: bool
14
14
"""
15
- trantab = s .maketrans ("()[]{}" , "142536" )
16
- s = s .translate (trantab )
17
- s = list (map (int , s ))
18
- stack = []
15
+ left = {'[' : 1 , '(' :2 , '{' :3 }
16
+ right = {']' :1 , ')' :2 , '}' :3 }
17
+ left_s = []
19
18
for i in s :
20
- if i < 4 :
21
- stack .append (i )
19
+ if i in left :
20
+ left_s .append (left [ i ] )
22
21
else :
23
- try :
24
- if i - stack .pop () == 3 :
25
- pass
26
- else :
27
- return False
28
- except IndexError :
22
+ if len (left_s ) == 0 :
23
+ return False
24
+ if left_s .pop () != right [i ]:
29
25
return False
30
- else :
31
- return len (stack )== 0
26
+ return len (left_s ) == 0
32
27
33
28
34
29
if __name__ == "__main__" :
@@ -60,4 +55,6 @@ def isValid(self, s):
60
55
return False
61
56
stack.pop()
62
57
return len(stack) == 0
58
+
59
+
63
60
'''
You can’t perform that action at this time.
0 commit comments