EasyPythonJavaScriptTypeScript

Balanced Brackets

Data StructuresStackStrings

Implement solve(str) that returns true when every opening bracket in
str is closed by the correct matching bracket in the correct order, and
false otherwise.

Supported pairs: () [] {}

Non-bracket characters are ignored.

Examples

solve("({[]})")  → true
solve("()[]{}")  → true
solve("([)]")    → false   // interleaved — wrong order
solve("{[]")     → false   // unclosed opening
solve("")        → true    // empty string is balanced

Approach
Use a stack: push each opening bracket; on a closing bracket, pop and verify
the match. Return true only if the stack is empty at the end.

Constraints

  • Input may contain any Unicode characters; only ()[]{} matter.
  • Do NOT use a counter — counters cannot detect interleaved mismatches like ([)].

Sample tests

Test #1Properly nested mixed brackets
Input: ["({[]})"]
Output: true
Test #2Sequence of balanced groups including nested
Input: ["()[]{[()]}"]
Output: true
Test #3Interleaved brackets — wrong closing order
Input: ["([)]"]
Output: false