From d9e8dc7da3eccda0e97e1aa3b7c74b7c47fea8cf Mon Sep 17 00:00:00 2001 From: StanKatsyuk Date: Fri, 8 Sep 2023 15:29:26 -0700 Subject: [PATCH] Init --- python/Stack/Valid Parantheses.py | 51 ------------------------------- python/Stack/Valid Parentheses.py | 41 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 51 deletions(-) delete mode 100644 python/Stack/Valid Parantheses.py create mode 100644 python/Stack/Valid Parentheses.py diff --git a/python/Stack/Valid Parantheses.py b/python/Stack/Valid Parantheses.py deleted file mode 100644 index 40ae3274c..000000000 --- a/python/Stack/Valid Parantheses.py +++ /dev/null @@ -1,51 +0,0 @@ -""" -Given a string s containing algebraic expressions which contains alphabets from A-Z and parentheses '(', ')', -'{', '}', '[' and ']', determine if the input string is valid. - -An input string is valid if: - Open brackets must be closed by the same type of brackets. - Open brackets must be closed in the correct order. - -Examples: - (A+B)+(C-D) : Valid as expression has brackets which have same type of opening and closing brackets - (A+B)+(C- : Invalid as closing brace is missing - {(A+B)+[C-D]} : Valid as opening and closing brackets are of same type - ((A+B)-[C+D]} : Invalid as The last closing bracket does not correspond to opening bracket -""" - -from python.Stack.Stack import Stack - -""" - Time Complexity : O(n) due to traversal of the expression string - Space Complexity : O(n) due to stack -""" - - -def check_valid_parentheses(expression): - st = Stack() - valid = True - - for i in expression: - if i in "({[": - st.push(i) - elif i in ")}]": - if st.is_empty(): - valid = False - break - elif (i == ")" and st.peek() != "(") or (i == "}" and st.peek() != "{") or (i == "]" and st.peek() != "["): - valid = False - break - else: - st.pop() - - if not st.is_empty(): - valid = False - - return valid - - -if __name__ == "__main__": - print(check_valid_parentheses("{(A+B)+[C-D]}")) # Valid - print(check_valid_parentheses("(A+B)+(C-")) # Invalid - print(check_valid_parentheses("((A+B)-[C+D]}")) # Invalid - print(check_valid_parentheses("}(A-B){")) # Invalid diff --git a/python/Stack/Valid Parentheses.py b/python/Stack/Valid Parentheses.py new file mode 100644 index 000000000..7e99e3328 --- /dev/null +++ b/python/Stack/Valid Parentheses.py @@ -0,0 +1,41 @@ +""" +Given a string containing algebraic expressions with alphabets (A-Z) and specific parentheses ('(', ')', '{', '}', '[', ']'), +determine if the input string is valid. + +An input string is valid if: + - Open brackets must be closed by the same type of brackets. + - Open brackets must be closed in the correct order. + +Examples: + (A+B)+(C-D) : Valid as the expression has matching opening and closing brackets. + (A+B)+(C- : Invalid as a closing bracket is missing. + {(A+B)+[C-D]} : Valid as opening and closing brackets match correctly. + ((A+B)-[C+D]} : Invalid as the last closing bracket does not correspond to an opening bracket. +""" + +from python.Stack.Stack import Stack + +""" + Time Complexity : O(n) due to traversal of the expression string + Space Complexity : O(n) due to stack +""" + +def check_valid_parentheses(expression): + stack = Stack() + parentheses_map = {')': '(', '}': '{', ']': '['} + + for char in expression: + if char in parentheses_map.values(): + stack.push(char) + elif char in parentheses_map: + if stack.is_empty() or stack.pop() != parentheses_map[char]: + return False + + return stack.is_empty() + + +if __name__ == "__main__": + print(check_valid_parentheses("{(A+B)+[C-D]}")) # Valid + print(check_valid_parentheses("(A+B)+(C-")) # Invalid + print(check_valid_parentheses("((A+B)-[C+D]}")) # Invalid + print(check_valid_parentheses("}(A-B){")) # Invalid