File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ class ArrayStack :
2+ def __init__ (self , capacity ):
3+ self .capacity = capacity
4+ self .array = [None ] * self .capacity
5+ self .top = - 1
6+
7+ def isEmpty (self ):
8+ return self .top == - 1
9+
10+ def isFull (self ):
11+ return self .top == self .capacity - 1
12+
13+ def push (self , item ):
14+ if not self .isFull ():
15+ self .top += 1
16+ self .array [self .top ] = item
17+ else :
18+ pass
19+
20+ def pop (self ):
21+ if not self .isEmpty ():
22+ self .top -= 1
23+ return self .array [self .top + 1 ]
24+ else :
25+ pass
26+
27+ class Solution :
28+ def isValid (self , s : str ) -> bool :
29+ stack = ArrayStack (100 )
30+ for ch in s :
31+ if ch in ('(' , '[' , '{' ):
32+ stack .push (ch )
33+ elif ch in (')' , ']' , '}' ):
34+ if stack .isEmpty ():
35+ return False
36+ else :
37+ left = stack .pop ()
38+ if (ch == ')' and left != '(' ) or \
39+ (ch == ']' and left != '[' ) or \
40+ (ch == '}' and left != '{' ):
41+ return False
42+ return stack .isEmpty ()
43+
44+
You can’t perform that action at this time.
0 commit comments