FastPrepMaximum Parenthesis Nesting Depth

Maximum Parenthesis Nesting Depth

Rippling logoRippling● EasyINTERNPHONE SCREEN
Learn

Problem statement

Given a string expression with balanced parentheses, return its maximum nesting depth: the greatest number of open parenthesis pairs at any position.

Only ( and ) affect the depth. All other characters are ignored. An expression with no parentheses has depth 0.

Function

maxDepth(expression: String) → int

Examples

Example 1

expression = "a*(b+(c*d))"return = 2

The parentheses around c*d are inside one other pair.

Example 2

expression = "()(())"return = 2

Separate groups do not add their depths; the deepest group has two pairs.

Example 3

expression = "plain text"return = 0

There is no open parenthesis.

Constraints

  • 0 <= expression.length <= 10^5.
  • The string contains printable ASCII characters.
  • Its parentheses are balanced.

More Rippling problems

See Rippling hiring insights
public int maxDepth(String expression) {
    // Write your code here.
}
expression"a*(b+(c*d))"
expected2
Checking account…