File tree Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change 1
1
# class Solution(object):
2
2
# def isValid(self, s):
3
-
3
+
4
4
#
5
5
class Solution :
6
6
def isValid (self , s ):
@@ -40,6 +40,23 @@ def isValid(self, s):
40
40
else :
41
41
return False
42
42
43
+ def isValid_stack (self , s ):
44
+ stack = []
45
+ for i in s :
46
+ if i in ['(' , '{' , '[' ]:
47
+ stack .append (i )
48
+ else :
49
+ if len (stack ) == 0 :
50
+ return False
51
+ out_stack = stack .pop ()
52
+ if not ((out_stack == '(' and i == ')' ) or \
53
+ (out_stack == '[' and i == ']' ) or \
54
+ (out_stack == '{' and i == '}' )):
55
+ return False
56
+ if len (stack ) != 0 : # make sure it is not single side column
57
+ return False
58
+ else :
59
+ return True
43
60
44
61
# def isValid(self, s):
45
62
# # python replace
@@ -57,3 +74,11 @@ def isValid(self, s):
57
74
# return True
58
75
# else:
59
76
# return False
77
+
78
+
79
+ if __name__ == '__main__' :
80
+ sinput = "()[]{}"
81
+ gt = True
82
+ s = Solution ()
83
+ out = s .isValid_stack (sinput )
84
+ print (out )
You can’t perform that action at this time.
0 commit comments