PROBLEM 14 OF 75

Valid Parentheses

Decide whether brackets close in the correct type and order.

PATTERNStacksThe newest unresolved item is the first one you must close.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Decide whether brackets close in the correct type and order.

WHY THIS FITS

A closing bracket must match the newest unmatched opening bracket.

COMPLEXITY

O(n) time · O(n) space

COMMON FAILURE

Checking only total counts ignores nesting order.

THE REASONING, IN ONE PLACE

How to solve Valid Parentheses

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Decide whether brackets close in the correct type and order.

Why Stacks fits

A closing bracket must match the newest unmatched opening bracket.

State to maintain

A stack of expected closing brackets.

Transition

Push the expected closer for each opener; every closer must equal the stack top.

Time and space

O(n) time · O(n) space

Counterexample to the tempting mistake

Checking only total counts ignores nesting order.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗