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
falsewhen a closing bracket appears too early. - Return
falsewhen an opening bracket remains unmatched. - Return
trueonly when every bracket pair is valid.
Example 1
Input
s (string) =
({[]})Returns (boolean)
trueExample 2
Input
s (string) =
([)]Returns (boolean)
falseExample 3
Input
s (string) =
a+(b*c)-{d/e}Returns (boolean)
truePush 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