Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-355

Bracket Nesting Depth

Problem

Scan a string of (), [], and {} characters. Return [max_depth, balanced] where depth counts currently open brackets and max_depth is its maximum, and balanced is True only when every opening bracket is closed by the matching type in order with nothing left over. An unmatched closing bracket is ignored for depth but makes balanced False.

Hint

A closing bracket only reduces the depth when it matches the top of the stack.

Test cases

case 1
in: ["(()())"]
expected: [2,true]
case 2
in: ["([{}])"]
expected: [3,true]
case 3
in: ["(()"]
expected: [2,false]
case 4
in: [")("]
expected: [1,false]
case 5
in: [""]
expected: [0,true]
Starter codePython
def bracket_depth(s):
    # Your code here
    pass

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