JavaMediumStack & Queue20 min

Validate balanced brackets

Print true if every closing bracket matches the most recent unmatched opening bracket and no openings remain. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe algorithm selection, invariants, and boundary handling.

Input

One line containing only (), [], and {}, possibly empty.

Output

Print true if every closing bracket matches the most recent unmatched opening bracket and no openings remain.

Constraints

0 <= length <= 100000.

Example

Input

{[()]}

Output

true

Example 2 input

}{

Output

false

This example exercises a further boundary or ordering case.

Public test cases (2)

  • Input

    {[()]}

    Expected output

    true
  • Input

    ([)]

    Expected output

    false
Hints (2)
  1. Identify the state needed to avoid recomputing work for each element.
  2. Use an ArrayDeque as a stack to enforce last-opened, first-closed matching.

Target complexity: time O(n) · space O(n)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All Java programs