Stack
TimeO(n)SpaceO(n)
Scan the string, pushing every opening bracket. Each closing bracket must match the bracket on top of the stack — otherwise the string is invalid.
Key idea. A stack matches each closing bracket to the most recent opener. LIFO order is exactly what nesting requires.
1
Loading…
—