Pattern visualizer
Stack matching
A stack remembers openers in last-in-first-out order, so the top is always the newest bracket still waiting to close. Reach for it whenever the most recent unfinished thing must finish first — matching brackets, undo, nested calls. Animated on: Valid Parentheses on "({[]})".
Stack
step 1 / 11
(
[0]{
[1][
[2]]
[3]}
[4])
[5]line 2
Input "({[]})". Valid means every closer matches the newest unclosed opener. A stack tracks exactly that. Stack: empty.
Pseudocode
1FUNCTION isValid(s):2 make an empty stack3 FOR each position i from 0 to the length of s:4 ch = s[i]5 IF ch is an opener '(' or '{' or '[':6 push ch onto the stack7 ELSE IF popping the top does not equal the matching opener of ch:8 RETURN false9 END IF10 END FOR11 RETURN whether the stack is now empty12END FUNCTION
← / → step · space play · Home restart