Skip to content

Commit a09d2c2

Browse files
committed
stack apps added
1 parent c956dff commit a09d2c2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

0 commit comments

Comments
 (0)