1614. Maximum Nesting Depth of the Parentheses

1614. Maximum Nesting Depth of the Parentheses #

Problem #

A string is a valid parentheses string (denoted VPS) if it meets one of the following:

  • It is an empty string "", or a single character not equal to "(" or ")",
  • It can be written as AB (A concatenated with B), where A and B are VPS’s, or
  • It can be written as (A), where A is a VPS.

We can similarly define the nesting depth depth(S) of any VPS S as follows:

  • depth("") = 0
  • depth(C) = 0, where C is a string with a single character not equal to "(" or ")".
  • depth(A + B) = max(depth(A), depth(B)), where A and B are VPS’s.
  • depth("(" + A + ")") = 1 + depth(A), where A is a VPS.

For example, """()()", and "()(()())" are VPS’s (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS’s.

Given a VPS represented as string s, return the nesting depth of s.

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

Example 3:

Input: s = "1+(2*3)/(2-1)"
Output: 1

Example 4:

Input: s = "1"
Output: 0

Constraints:

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

Summary #

If a string satisfies one of the following conditions, it can be called a valid parentheses string (abbreviated as VPS):

  • The string is an empty string “”, or a single character that is not “(” or “)”.
  • The string can be written as AB (A concatenated with B), where both A and B are valid parentheses strings.
  • The string can be written as (A), where A is a valid parentheses string.

Similarly, the nesting depth depth(S) of any valid parentheses string S can be defined as:

  • depth("") = 0
  • depth(C) = 0, where C is a string with a single character, and that character is not “(” or “)”
  • depth(A + B) = max(depth(A), depth(B)), where both A and B are valid parentheses strings
  • depth("(” + A + “)") = 1 + depth(A), where A is a valid parentheses string

For example: “”, “()()”, and “()(()())” are all valid parentheses strings (with nesting depths of 0, 1, and 2 respectively), while “)(” and “(()” are not valid parentheses strings. Given a valid parentheses string s, return the nesting depth of s.

Solution Approach #

  • Easy problem. Find the nesting depth of a parentheses string. The parentheses strings given in the problem are all valid, so there is no need to consider invalid cases. Scan the parentheses string once; when encountering (, simply increment, and dynamically maintain the maximum value; when encountering ), simply decrement. Finally, output the maximum value.

Code #

package leetcode

func maxDepth(s string) int {
	res, cur := 0, 0
	for _, c := range s {
		if c == '(' {
			cur++
			res = max(res, cur)
		} else if c == ')' {
			cur--
		}
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文