Maximum Parenthesis Nesting Depth
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) → intExamples
Example 1
expression = "a*(b+(c*d))"return = 2The parentheses around c*d are inside one other pair.
Example 2
expression = "()(())"return = 2Separate groups do not add their depths; the deepest group has two pairs.
Example 3
expression = "plain text"return = 0There is no open parenthesis.
Constraints
0 <= expression.length <= 10^5.- The string contains printable ASCII characters.
- Its parentheses are balanced.