Valid Parentheses
FreeStackEasy16 of 75
The problem
Given a string made of (), [] and {}, decide whether every opener has a matching closer in the correct nested order. The empty string is valid.
Example
"{[()]}" → true; "([)]" → false
Need a hint?
The next closing bracket must match the most recent unmatched opener.
Write pseudocode, trace the example, or note an edge case. This scratchpad does not run code.
Notes stay in this browser when storage is available.
Read the solution approach
Push opening brackets onto a stack. For a closing bracket, reject an empty stack or a mismatched top; otherwise pop. Accept only if the stack is empty at the end, since leftover openers are unmatched.
Complexity
O(n) time and O(n) space.
Before moving on, explain why the algorithm is correct and trace a boundary case without looking at the approach.