Intermediate Level

Implement the method isBalancedBrackets that checks whether brackets are correctly nested.

Check matching and nesting for (), [], and {}. Other characters should be ignored.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Return false when a closing bracket appears too early.
  • Return false when an opening bracket remains unmatched.
  • Return true only when every bracket pair is valid.

Example 1

Input
s (string) = ({[]})
Returns (boolean) true

Example 2

Input
s (string) = ([)]
Returns (boolean) false

Example 3

Input
s (string) = a+(b*c)-{d/e}
Returns (boolean) true

Push opening brackets on a stack and match every closing bracket against the latest opening bracket.

Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.