Skip to main content
Back to problems
Leetcode
Easy
Strings
Stacks
Simulation
Maximum Nesting Depth of the Parentheses

Compute the maximum number of nested parentheses in a valid expression.

Acceptance 0%
Problem Statement

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.

Input Format

  • A single string s
  • s contains lowercase English letters, (, and )
  • The string is assumed to be a valid parenthesized expression

Output Format

  • Return an integer: the maximum nesting depth of the parentheses in s

Constraints

  • 1 <= s.length <= 1000 in typical interview settings
  • s is valid: parentheses are balanced
  • Only lowercase letters and parentheses appear in the string
Examples
Sample cases returned by the problem API.

Example 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

Unlock deeper context for this problem

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.