TypeScriptMediumStack & Queue25 min

Validate balanced brackets

Return whether a string consisting only of parentheses, square brackets, and braces is properly nested. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.

Input

One JSON value: string. See the sample for its exact shape.

Output

One compact JSON value of type boolean. Strings include JSON quotes; arrays preserve the specified order.

Constraints

Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range.

Example

Input

"([]){}"

Output

true

Example 2 input

"{{[()]}}"

Output

true

Checks a boundary or contrasting case beyond the public examples.

Public test cases (2)

  • Input

    "([]){}"

    Expected output

    true
  • Input

    "([)]"

    Expected output

    false
Hints (2)
  1. Start with the required output order and the empty-input behavior.
  2. Use a stack to match each closing bracket to the most recent opener

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

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All TypeScript programs