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
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.
| Aspect | Value |
|---|---|
| Pattern | Stack / monotonic stack |
| Recognise it by | Matched, properly nested brackets. |
| Time complexity | O(n) |
| Space complexity | O(n) |
| Difficulty | Easy |
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.