From 2af624ff017b0f1d9bf1d9cb26a8eb022f6fdd1e Mon Sep 17 00:00:00 2001 From: Omkar Pathak Date: Sun, 16 Jul 2017 11:26:11 +0530 Subject: [PATCH] Checking balanced parantheses using Stack --- .../Stacks/Balanced_Parentheses.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 data_structures/Stacks/Balanced_Parentheses.py diff --git a/data_structures/Stacks/Balanced_Parentheses.py b/data_structures/Stacks/Balanced_Parentheses.py new file mode 100644 index 000000000..6b7740380 --- /dev/null +++ b/data_structures/Stacks/Balanced_Parentheses.py @@ -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