Loading content...
You are given a well-formed expression string s that contains parentheses along with other characters such as digits and arithmetic operators. Your task is to determine the deepest level of nested parentheses present in the string.
The nesting depth at any point in the string refers to how many unclosed opening parentheses ( currently surround that position. The deepest bracket level is the maximum nesting depth observed anywhere in the string.
A well-formed parentheses string (also known as a Valid Parentheses String or VPS) satisfies the following conditions:
A + B, where A and B are both valid parentheses strings (concatenation), or(A), where A is a valid parentheses string.Your goal is to compute and return the maximum depth of parenthesis nesting in the given expression.
s = "(1+(2*3)+((8)/4))+1"3The number 8 is enclosed within 3 levels of nested parentheses: the outermost group (1+(2*3)+((8)/4)), then ((8)/4), and finally the innermost (8). No other position in the string has a deeper nesting level, so the answer is 3.
s = "(1)+((2))+(((3)))"3The digit 3 is wrapped inside 3 nested pairs of parentheses: first (((3))), then ((3)), and innermost (3). This represents the deepest nested position in the entire expression.
s = "()(())((()()))"3Analyzing the structure: () has depth 1, (()) has depth 2 at its innermost, and ((()()) has depth 3 at the () inside the (()()) segment. The maximum depth encountered is 3.
Constraints