Skip to content

Commit 2af624f

Browse files
committed
Checking balanced parantheses using Stack
1 parent ce3e91a commit 2af624f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Author: OMKAR PATHAK
2+
3+
import Stack
4+
5+
def parseParenthesis(string):
6+
balanced = 1
7+
index = 0
8+
myStack = Stack.Stack(len(string))
9+
while (index < len(string)) and (balanced == 1):
10+
check = string[index]
11+
if check == '(':
12+
myStack.push(check)
13+
else:
14+
if myStack.isEmpty():
15+
balanced = 0
16+
else:
17+
myStack.pop()
18+
index += 1
19+
20+
if balanced == 1 and myStack.isEmpty():
21+
return True
22+
else:
23+
return False
24+
25+
if __name__ == '__main__':
26+
print(parseParenthesis('((()))')) # True
27+
print(parseParenthesis('((())')) # False

0 commit comments

Comments
 (0)