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
+ #Program to validate parenthesis
2
+ #Using Stack
3
+
4
+ class Stack :
5
+
6
+ def __init__ (self ):
7
+ self .stack = []
8
+
9
+ def push (self , item ):
10
+ self .stack .append (item )
11
+
12
+ def pop (self ):
13
+ return self .stack .pop ()
14
+
15
+ def isEmpty (self ):
16
+ return self .stack == []
17
+
18
+ def top (self ):
19
+ if (self .size ()!= 0 ):
20
+ item = self .stack [self .size ()- 1 ]
21
+ return item
22
+
23
+ def size (self ):
24
+ return len (self .stack )
25
+
26
+
27
+ def parenthesisCheck (string ):
28
+ """ Function to check parenthesis using Stack class """
29
+ s = Stack ()
30
+ length = len (string )
31
+
32
+ for x in range (0 , length ):
33
+ if (string [x ] == '(' ):
34
+ s .push (string [x ])
35
+ elif (string [x ] == ')' and s .isEmpty () == False ):
36
+ s .pop ()
37
+
38
+ if (s .isEmpty ()):
39
+ return True
40
+ else :
41
+ return False
42
+
43
+ print (parenthesisCheck ("(()(()" )) #False
44
+ print (parenthesisCheck ("(())" )) #True
You can’t perform that action at this time.
0 commit comments