Valid Parantheses
Problem Statement
Given a string str
containing just the characters (
, )
, {
, }
, [
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.
- Every close bracket has a corresponding open bracket of the same type.
Stack solution
Being a LIFO data structure, we can know which was the latest inserted element. We use this property to know if there was a matching opening brace last inserted for current closing brace
-
A trivial edge case is that all odd-length strings would be invalid
-
Our approach is that we only push opening braces into the stack.
-
Whenever we encounter a closing brace, there should be a matching opening brace present at the top of stack. Then we can clear out current matching braces. If there wasn’t a matching opening brace at top of stack for current closing brace, then the input is unbalanced
-
Make sure to check stack isn’t empty before accessing or popping the top element
-
At the end, all pairs would clear out and stack should become empty in the case of balanced pairs. If stack isn’t empty at the end, then the input is unbalanced.
Time
We are iterating all string characters once, so \( O(n) \) time complexity
Space
At most, there would be \( n \) characters present inside stack. This would occur when all characters are opening braces. Hence, the space complexity is \( O(n) \)