0020. Valid Parentheses
Interactive visualization of bracket validation algorithms
Stack Approach
Replace Pairs Approach
String:
()
()[]{}
(]
{[()]}
([)]
(({{[[]]}}))
empty
(
]{{}
Step
Play
Reset
Speed:
5
Stack Approach:
Push opening brackets onto the stack. For each closing bracket, check if it matches the top of the stack. Time:
O(n)
, Space:
O(n)
Stack
empty
Click "Step" or "Play" to begin...
Waiting to start...
Replace Pairs Approach:
Repeatedly remove
()
,
[]
,
{}
pairs until no more can be removed. If the string is empty, it's valid. Time:
O(n²)
, Space:
O(n)
Click "Step" or "Play" to begin...
Waiting to start...