Skip to main content
All Visualizers
#LC 20•Stack•Easy

Valid Parentheses

Given a string of brackets, decide whether every opening bracket is closed by the matching bracket in the correct nested order.

Understand the problem

Given a string of brackets, decide whether every opening bracket is closed by the matching bracket in the correct nested order.

Example 1

Input: s = "{[]}"

Output: true

The inner square brackets close first, then the outer curly braces.

Example 2

Input: s = "([)]"

Output: false

The closing parenthesis does not match the latest opening square bracket.

Constraints

  • The string contains only (, ), [, ], {, and }.
  • The input length is between 1 and 10,000 characters.
Think first

When a closing bracket appears, which opening bracket should it match?

Pattern tags:StackNested pairsEarly mismatch detection
Time: O(n)Space: O(n)
Step 1 of 6 • Use Left/Right arrows or Space to control