Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-118

Constituency Bracket Balance

Problem

Check whether a bracketed constituency string has balanced parentheses.

Return True when every opening parenthesis is closed in the correct order, and a negative depth at any point means failure. An empty string is balanced.

Hint

Track the nesting depth and fail as soon as it goes negative.

Test cases

case 1
in: ["(A (B) C)"]
expected: true
case 2
in: ["(A))"]
expected: false
case 3
in: [""]
expected: true
case 4
in: ["(("]
expected: false
Starter codePython
def bracket_balance_check(text):
    # Your code here
    pass

4 test cases · run your code against them in the editor