Parentheses
Parentheses problems involve validating, balancing, and manipulating bracket sequences. These problems test stack usage, greedy algorithms, and string manipulation skills.Core Concepts
Bracket Types
- Round: ( )
- Square: [ ]
- Curly:
- Angle: < > (less common)
Key Properties
- Valid: Every opening has matching closing in correct order
- Balanced: Equal number of opening and closing
- Well-formed: Properly nested, no interleaving
Key Patterns & Techniques
1. Stack for Validation
- Push opening brackets
- Pop and match closing brackets
- Stack empty at end = valid
- Time: O(n), Space: O(n)
2. Greedy Counting
- Track balance (open - close)
- Balance never negative, ends at 0
- Faster than stack for single type
- Time: O(n), Space: O(1)
3. Two-Pass Validation
- Left-to-right: track open parentheses
- Right-to-left: track close parentheses
- Both passes must succeed
4. BFS/DFS for Generation
- Generate all valid combinations
- Backtracking with constraints
- Remove minimum for validity
Common Approaches
Valid Parentheses (Stack)
Minimum Add to Make Valid
Minimum Remove to Make Valid
Remove Invalid Parentheses (BFS)
Longest Valid Parentheses
Problems in This Category
020. Valid Parentheses
Easy | Frequency: 74.9%Stack-based validation for multiple bracket types.
001. Minimum Remove to Make Valid
Medium | Frequency: 100.0%Two-pass algorithm to mark and remove invalid parentheses.
052. Minimum Add to Make Valid
Medium | Frequency: 51.9%Greedy counting of unmatched parentheses.
056. Remove Invalid Parentheses
Hard | Frequency: 47.0%BFS to find all valid strings with minimum removals.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | When to Use |
|---|---|---|---|
| Stack Validation | O(n) | O(n) | Multiple bracket types |
| Greedy Count | O(n) | O(1) | Single bracket type |
| Two-Pass | O(n) | O(1) or O(n) | Mark and remove |
| BFS Generation | O(2^n) | O(n) | Find all valid strings |
| DP | O(n²) | O(n) | Longest valid substring |
Interview Tips
Pattern Recognition
- “Valid parentheses?” → Stack for multiple types, counting for single
- “Minimum add/remove” → Greedy counting or two-pass
- “All valid strings” → BFS/DFS with minimum removals
- “Longest valid substring” → Stack or DP
- “Generate all valid” → Backtracking with constraints
Common Mistakes
- Not handling multiple bracket types correctly
- Forgetting to check if stack is empty before popping
- Not checking if final stack is empty
- Confusing “add” vs “remove” operations
- Not considering empty string as valid
Key Insights
- Stack for validation - classic pattern for nested structures
- Greedy for single type - O(1) space, track open count
- Balance never negative - more ’)’ than ’(’ is invalid
- Two-pass for removal - mark invalid in both directions
- BFS for minimum - level-by-level finds minimum removals
- DP for longest - track valid substring lengths
Validation Approaches
Stack Method (Multiple Types)
Counter Method (Single Type)
Advanced Patterns
Generate Parentheses (Backtracking)
Score of Parentheses
Different Ways to Add Parentheses
Valid Parenthesis String (Wildcard)
Decision Tree
Pro Tip: For validation, use stack when you have multiple bracket types. For single type, greedy counting with O(1) space is better. For removal problems, mark invalid brackets in two passes (left-to-right and right-to-left) to handle both directions.