Ctrl K

Deepest Bracket Level

Easy
A calculator displays a formula formula whose brackets are properly matched: every opening bracket has its own closing one, and no closing bracket comes before its opening partner. The depth of a character is the number of bracket pairs that enclose it at once. Return the greatest depth reached anywhere in the formula. If there are no brackets, the answer is 0.
Example 1
formula="(x-(4*y))/((2+(z)))" → 3

The letter z sits inside three pairs of brackets: ((2+(z))). Nothing goes deeper.

Example 2
formula="7+a*b" → 0

There are no brackets at all, so the depth is 0.

Example 3
formula="()(())()" → 2

The deepest point is inside (()), two levels down.

Constraints
  • 0 ≤ len(formula) ≤ 200
  • formula consists of digits, lowercase English letters, the signs +, -, *, / and the brackets (, )
  • The brackets in formula are properly matched
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBracket Balance EasySame Text After Erasing EasyPeel the Outer Brackets EasyPopping Pairs EasyCase Pairs Cancel Out Easy
loading Python…
Input
formula="(x-(4*y))/((2+(z)))"
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 67 tests on the server, hidden ones included.