Balanced Parentheses
Push openers and pop on closers; balanced when every closer matches the top and the stack ends empty.
Decision · step 2 of 10Stacks for Expressions: Balanced brackets
'{' at 0. An opener: push it. The stack is now {.
What you will see
Characters scan left to right; brackets drop onto the stack and pop when matched.
Cost
| Best | O(n) |
|---|---|
| Average | O(n) |
| Worst | O(n) |
| Space | O(n) |
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next, try operations in any order.
Screen readers: Items announce position from the top or front and value; each step announces the operation and the new top or front.
Reduced motion: Items appear or disappear in place with a crossfade instead of sliding in or out.
Related
Taught by the same lesson
Stacks for Expressions covers these too, in the same run.