Easy
Valid Parentheses
A stack matches each closer to the most recent opener.
Approach
Push every opening bracket. On a closing bracket, the top of the stack must be its matching opener — otherwise it's invalid. After the scan the stack must be empty. The stack naturally captures the last-opened-first-closed nesting.
Time complexity
O(n)
Space complexity
O(n)
Common mistake
Only counting brackets rather than matching types/order — '([)]' has balanced counts but is invalid.
See it run, step by step
Generate an interactive lesson for Valid Parentheses — trace every variable and watch the algorithm execute until it clicks.
Start a lesson on Valid Parentheses