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.
ss contains lowercase English letters, (, and )s1 <= s.length <= 1000 in typical interview settingss is valid: parentheses are balancedExample 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
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.