Introduction
The Valid Parentheses problem involves checking whether brackets in a string are balanced correctly.
Given a string s containing:
( ) { } [ ]the task is to determine:
- whether opening brackets
and closing brackets
are properly matched
A valid string must satisfy:
- Every opening bracket
must have a closing bracket - Brackets must close
in correct order
This problem helps in understanding:
- stacks
- balanced expressions
- traversal techniques
- matching logic
Example
Input:s = "()[]{}"
Output: True
Explanation: All brackets are balanced.
Input: s = "(]"
Output: False
Explanation: Opening and closing brackets do not match.
Constraints
1 <= s.length <= 10^4s contains only:
( ) { } [ ]
Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Repeatedly remove:
- ()
- {}
- []
- Continue until:
- no valid pair exists
- If string becomes empty:
- valid parentheses
Otherwise:
- invalid parentheses
This approach works but repeated replacement increases time complexity.
Steps
- Traverse string repeatedly.
- Remove valid bracket pairs.
- Continue until no changes occur.
- If string becomes empty:
- return true
- Otherwise:
- return false
Dry Run
Input:s = "({[]})"Remove:
[]
String becomes: ({})
Remove: {}
String becomes:
()
Remove: ()
String becomes: ""
Final Result: True
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)Explanation:Repeated string replacement is performed.
Space Complexity: O(n) Explanation:
Extra string modifications are used.
Approach 2 : Optimized Solution (Stack)
Explanation
The optimized solution uses a Stack.
The idea is:
- Push opening brackets into stack
- When closing bracket appears:
- check top of stack
- If brackets match:
- pop stack
- Otherwise:
- invalid expression
This is the standard stack-based solution.
Steps
- Create empty stack.
- Traverse string characters.
- Push opening brackets.
- Check matching closing brackets.
- Pop matching brackets.
- If stack becomes empty:
- valid parentheses
Dry Run
Input:s = "({[]})"
Traverse (: Push into stack
Stack: (
Traverse {:
Push into stack
Stack: ( {
Traverse [:
Push into stack
Stack: ( { [ Traverse ]: Match found Pop [
Stack: ( {
Continue traversal... Stack becomes empty
Final Result: True
Optimized Code
Approach 2 : Optimized Solution (Stack)
Time Complexity:O(n)
Explanation:
Each bracket is processed once.
Space Complexity: O(n)
Explanation:
Extra stack is used.Edge Cases
- Empty string
- Only opening brackets
- Only closing brackets
- Incorrect nesting
- Multiple balanced groups
Why This Problem is Important
This problem helps in understanding:
- Stack data structure
- Balanced expression checking
- Matching logic
- Traversal techniques
- Nested structure handling
It is one of the most important stack interview problems.
This problem helps in understanding:
- Stack data structure
- Balanced expression checking
- Matching logic
- Traversal techniques
- Nested structure handling
It is one of the most important stack interview problems.
Real-World Applications
Balanced expression concepts are used in:
- Compiler design
- Syntax validation
- Code editors
- Expression evaluation
- HTML/XML parsing
Balanced expression concepts are used in:
- Compiler design
- Syntax validation
- Code editors
- Expression evaluation
- HTML/XML parsing
Common Mistakes
- Forgetting empty stack check
- Incorrect bracket matching
- Missing final stack validation
- Wrong push/pop order
- Forgetting empty stack check
- Incorrect bracket matching
- Missing final stack validation
- Wrong push/pop order
Interview Tips
Interviewers often expect:
- Stack-based optimization
- Proper matching logic
- O(n) solution explanation
Always explain why:
- opening brackets are pushed
- closing brackets validate stack top
Interviewers often expect:
- Stack-based optimization
- Proper matching logic
- O(n) solution explanation
Always explain why:
- opening brackets are pushed
- closing brackets validate stack top
Related Questions
- Remove Adjacent Duplicates
- Min Stack
- Next Greater Element
- Evaluate Expression
- Daily Temperatures
- Remove Adjacent Duplicates
- Min Stack
- Next Greater Element
- Evaluate Expression
- Daily Temperatures
Final Takeaway
The Valid Parentheses problem is a fundamental stack problem that teaches balanced expression validation and nested structure handling techniques. Understanding this problem builds a strong foundation for advanced stack and parsing interview problems.
The Valid Parentheses problem is a fundamental stack problem that teaches balanced expression validation and nested structure handling techniques. Understanding this problem builds a strong foundation for advanced stack and parsing interview problems.