Valid Parentheses

Easy Stack & Queue AI round

s is a string of brackets: each character is one of the opening brackets ([{ or the closing brackets )]}.

s is balanced when all of its brackets can be split into pairs, each an opening bracket and a later closing bracket of the same kind, so that any two pairs either nest, one lying wholly between the other's two brackets, or do not overlap at all.

Return true if s is balanced, and false if it is not.

Example 1

Input: s = "{[]()}"
Output: true

The first { pairs with the last }, and the [ ] pair and the ( ) pair both lie inside it, side by side, without overlapping.

Example 2

Input: s = "[{}(])"
Output: false

Example 3

Input: s = "(()"
Output: false

Constraints

  • 1 ≤ s.length ≤ 10⁵
  • s holds only the characters ( ) [ ] { }

Watch the explanation

More Stack & Queue

All Stack & Queue problems