Loading practice problems…
Loading your practice workspace…
Loading code editor…
24 / 29
Sign in
Algorithms/Stack

Valid Parentheses

easy20 min

Given a string containing only parentheses, brackets, and braces, return true when every opening symbol is closed by the same type in the correct order.

An empty stack at the end means every opening symbol was matched.

Example 1

Inputs = "()[]{}"

Outputtrue

Each opening symbol is immediately followed by its matching closing symbol, so all three pairs are valid.

Example 2

Inputs = "([)]"

Outputfalse

The first closing parenthesis appears while the square bracket is still open. The pairs cross, so the string is invalid.

Constraints

  • 1 ≤ s.length ≤ 10,000
  • s contains only ()[]{}.
StackStrings