Maximum Nesting Depth of Parentheses
Problem statement
Given a string expression whose parentheses are balanced, return the maximum number of simultaneously open parentheses at any position.
Characters other than ( and ) do not affect the depth.
Function
maxDepth(expression: String) → intExamples
Example 1
expression = "(1+(2*3)+((8)/4))+1"return = 3The innermost parentheses around 8 are nested inside two other open pairs.
Example 2
expression = "(1)+((2))+(((3)))"return = 3The final term opens three parenthesis pairs before closing any of them.
Example 3
expression = "1+2"return = 0The expression contains no parentheses.
Constraints
1 <= expression.length <= 100000expressioncontains printable ASCII characters.- The parentheses in
expressionare balanced.