Talentd
FleetCode

1614. Maximum Nesting Depth of the Parentheses

Easy86.1% acceptance7 companies

Given a valid parentheses string s, return the nesting depth of s. The nesting depth is the maximum number of nested parentheses.

Example 1:

Input: s = "(1+(2*3)+((8)/4))+1"

Output: 3

Explanation:

Digit 8 is inside of 3 nested parentheses in the string.

Example 2:

Input: s = "(1)+((2))+(((3)))"

Output: 3

Explanation:

Digit 3 is inside of 3 nested parentheses in the string.

Example 3:

Input: s = "()(())((()()))"

Output: 3

Constraints:

  • 1 <= s.length <= 100
  • s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.
  • It is guaranteed that parentheses expression s is a VPS.

Hints open one at a time

Hint 1
The depth of any character in the VPS is the ( number of left brackets before it ) - ( number of right brackets before it )
4:08
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxDepth(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"(1+(2*3)+((8)/4))+1"
Input
"(1)+((2))+(((3)))"
Input
"()(())((()()))"
Your code is saved on this device.Solution