Strings Parentheses and nesting
134. Maximum Nesting Depth of the Parentheses
You are given a string s made of digits, the operators +, -, * and /, and parentheses, like a
formula. Its parentheses are properly nested: every ( is closed by a later ), and every ) closes an
earlier (.
Return the nesting depth of s: the largest number of pairs of parentheses that sit one inside the
next. In (1+(2*3))+(4), the pair around 2*3 sits inside the first pair, so the depth is 2. A string
with no parentheses has depth 0.
Example 1
Input: s = "(4+(2*(7-1)))/(3+5)"
3Explanation: 7-1 sits inside three pairs: its own, the one around 2*(7-1), and the outer one. No character sits
deeper.
Example 2
Input: s = "(1+2)*(3-4)/(5)"
1Explanation: There are three pairs, but they sit side by side. None of them is inside another.
Example 3
Input: s = "8*3-1"
0Explanation: There are no parentheses at all.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ s.length ≤ 105sholds only digits and the characters+,-,*,/,(and).- The parentheses in
sare properly nested.
Clarifying questions
What you would ask an interviewer before writing any code.