LC 20
LeetCode 20 · Easy

Valid Parentheses

Return true if every bracket is closed by the correct type in the correct order.


Try it

Step through the core mechanic. The simulator below runs the stack / monotonic stack shape this problem is built on.

Interactive · bracket matching push openers, pop on a matching closer
input
(
[
]
{
}
)
stack
empty
empty stack
step 1 / 8

The approach

Push each opening bracket; on a closing bracket, the stack top must be its match (else fail). A non-empty stack at the end means unclosed brackets.

AspectValue
PatternStack / monotonic stack
Recognise it byMatched, properly nested brackets.
Time complexityO(n)
Space complexityO(n)
DifficultyEasy

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.