Given a string s 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.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Constraints:
1 <= s.length <= 10^4sconsists of parentheses only'()[]{}'.
Algorithm:
- Use a stack to track opening brackets
- Use a hash map for O(1) bracket matching lookup
- For each character:
- If it's an opening bracket
(,[,{: push to stack - If it's a closing bracket
),],}:- Check if stack is empty (no matching opening bracket)
- Check if top of stack matches the expected opening bracket
- Pop the matched opening bracket
- If it's an opening bracket
- Return
trueonly if stack is empty after processing all characters
Time Complexity: O(n) - Single pass through the string
Space Complexity: O(n) - Stack can hold up to n/2 opening brackets in worst case
Implementation: See optimized.cpp
Algorithm:
- Use a stack to track opening brackets
- For each character:
- If it's an opening bracket: push to stack
- If it's a closing bracket:
- Check if stack is empty
- Check if top of stack matches the closing bracket using direct comparison
- Pop the matched opening bracket
- Return
trueonly if stack is empty
Time Complexity: O(n) - Single pass through the string
Space Complexity: O(n) - Stack can hold up to n/2 opening brackets in worst case
Implementation: See main.cpp
- LIFO (Last In, First Out) principle
- Perfect for tracking nested structures like parentheses
- Operations:
push(),pop(),top(),empty()
- O(1) average case lookup time
- Maps closing brackets to their corresponding opening brackets
- Reduces code complexity and improves readability
- Single pass algorithm - process each character exactly once
- Early termination - return false as soon as invalid condition is detected
- Forgetting to check if stack is empty before accessing top element
- Not handling the case where closing brackets appear before opening brackets
- Incorrect bracket matching logic - ensure proper pairing
- Not checking if stack is empty at the end - unmatched opening brackets
- 22. Generate Parentheses
- 32. Longest Valid Parentheses
- 301. Remove Invalid Parentheses
- 678. Valid Parenthesis String
- Nested structure: Parentheses can be nested within each other
- LIFO matching: The most recently opened bracket must be closed first
- State tracking: Stack maintains the current "depth" of nesting
- Hash map lookup: O(1) bracket matching vs O(1) but cleaner code
- Early termination: Return false immediately when invalid condition detected
- Single pass: Process each character exactly once
- Empty string (handled by constraint)
- Single character strings
- All opening brackets
- All closing brackets
- Mixed valid and invalid patterns