mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
Checking balanced parantheses using Stack
This commit is contained in:
parent
ce3e91a420
commit
2af624ff01
27
data_structures/Stacks/Balanced_Parentheses.py
Normal file
27
data_structures/Stacks/Balanced_Parentheses.py
Normal file
|
@ -0,0 +1,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('((()))')) # True
|
||||
print(parseParenthesis('((())')) # False
|
Loading…
Reference in New Issue
Block a user