AlgoScope

Balanced Parentheses

Push openers and pop on closers. Balanced when every closer matches the top and the stack ends empty.

Stack and queue algorithms, algorithm, beginner
Push {.instk{[()()]}{
Push {.instk{[()()]}{

Stacks for Expressions: Balanced brackets

Decision, step 2 of 10

'{' at 0. An opener: push it. The stack is now {.

Cost

Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(n)

Taught by the same lesson

Stacks for Expressions covers these too, in the same run.