Balanced Brackets in an Expression
Updated
Problem statement
Given a string expression, return true if its brackets are balanced and false otherwise.
The expression can use three kinds of brackets: round ( ), square [ ] and curly { }. The brackets are balanced when every opening bracket is later closed by a closing bracket of the same kind, every closing bracket closes an earlier opening bracket, and a pair that opens inside another pair also closes inside it. All other characters are ignored, so an expression with no brackets is balanced.
Function
isBalanced(expression: String) → booleanExamples
Example 1
expression = "{a+(b*c)}-[d]"return = true( opens inside { and closes before it. Then [ opens and closes. Every bracket has a partner of the same kind.
Example 2
expression = "(x+y]*2"return = falseThe round bracket ( is closed by a square bracket ], which is the wrong kind.
Example 3
expression = "[(m)]-(n"return = falseThe first part [(m)] is fine, but the last ( is never closed.
Constraints
1 <= expression.length <= 10^4expressionconsists of lowercase English letters, digits, spaces, the brackets( ) [ ] { }and the signs+ - * /
More Bank of America Online Assessment questions
- Return Exact Cash Register ChangeOA · Seen Aug 2026
- Sum Skipping Multiples of 7OA · Seen Nov 2025
- Wildcard Substring MatchOA · Seen Nov 2025
- Maximum String BeautyOA · Seen Sep 2024
- Merge Point of Two Linked ListsOA · Seen Oct 2023
- Remove Duplicates from a ListOA · Seen Oct 2023
- Pairs with Target Sum in a Sorted ArrayOA · Seen Oct 2023
- Count Level-Order Traversals of a TreeOA · Seen Sep 2023
- Count Set BitsOA · Seen Sep 2023
- Count Pairs with Sum KOA · Seen Jun 2023
Similar questions at other companies
- Validate a Bracket SequenceInMobi · OAInMobi interview questions
- Valid ParenthesesBlackRock · OABlackRock interview questions
- Valid ParenthesesVanguard · OAVanguard interview questions
- Valid Bracket SequenceNVIDIA · OA · PHONE SCREENNVIDIA interview questions
- Get Final StringIBM · OAIBM interview questions
- Backspace String CompareGoldman Sachs · OAGoldman Sachs interview questions
- The PromHackerEarth · OA
- Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
- Eliminate SubstringJP Morgan Chase · OAJP Morgan Chase interview questions
- Balancing ParenthesesOracle · OAOracle interview questions