forked from OmkarPathak/Data-Structures-using-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP02_BalancedParenthesis.py
More file actions
27 lines (23 loc) · 621 Bytes
/
P02_BalancedParenthesis.py
File metadata and controls
27 lines (23 loc) · 621 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Author: OMKAR PATHAK
import Stack
def parseParenthesis(string):
balanced = 1
index = 0
myStack = Stack.Stack(len(string))
while (index < len(string)) and (balanced == 1):
check = string[index]
if check == '(':
myStack.push(check)
else:
if myStack.isEmpty():
balanced = 0
else:
myStack.pop()
index += 1
if balanced == 1 and myStack.isEmpty():
return True
else:
return False
if __name__ == '__main__':
print(parseParenthesis('((()))'))
print(parseParenthesis('((())'))