Practice Valid Parentheses
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
A string contains only (), [], and {} brackets. Decide whether all brackets form a valid nesting: each closing bracket must match the type of the most recent unmatched opening bracket, and no bracket may remain unmatched. Adjacent complete groups are allowed. Practice approach: Push opening brackets on a stack. For a closer, reject an empty stack or a mismatched top; otherwise pop. Accept only if the stack is empty after the scan. Time and worst-case space are O(n).
Source: Valid Parentheses on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
s = "()"
true
# Example 2
s = "()[]{}"true
# Example 3
s = "(]"
false
# Example 4
s = "([])"
true
# Example 5
s = "([)]"
false
Constraints
Related LeetCode Problems
Practice more problems similar to Valid Parentheses.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.