Compute the maximum number of nested parentheses in a valid expression.
Given a string consisting of lowercase letters and the parentheses characters ( and ), determine the maximum nesting depth of the parentheses.
The nesting depth at any point is the number of currently open parentheses that have not yet been closed. Your task is to return the largest such depth reached while scanning the string from left to right.
This is a straightforward parsing and counting problem: letters do not affect the depth, while each opening parenthesis increases it and each closing parenthesis decreases it.
Input Format
- A single string
s scontains lowercase English letters,(, and)- The string is assumed to be a valid parenthesized expression
Output Format
- Return an integer: the maximum nesting depth of the parentheses in
s
Constraints
1 <= s.length <= 1000in typical interview settingssis valid: parentheses are balanced- Only lowercase letters and parentheses appear in the string
Example 1
Input
s = "(1+(2*3)+((8)/4))+1"
Output
3
Explanation
The deepest point is reached inside ((8)/4), where three parentheses are open at once.
Example 2
Input
s = "(1)+((2))+(((3)))"
Output
3
Explanation
The maximum depth is 3 in the last group of nested parentheses.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.