Maximum Nesting Depth of Parentheses

Problem Statement

Given a valid parentheses string s, your task is to find the maximum nesting depth of parentheses in the string.

  • The nesting depth of a string is the maximum number of parentheses that are open at the same time.
  • Only the characters ( and ) affect the nesting depth. Other characters are ignored.

Return 0 if the string is empty or contains no parentheses.

Examples

Input String Maximum Nesting Depth Description
"(1+(2*3)+((8)/4))+1" 3 Deepest level is ((8)/4) → depth 3
"(1)+((2))+(((3)))" 3 Increasing levels of nested brackets
"1+(2*3)/(2-1)" 1 Only one pair of parentheses at a time
"1+2-3" 0 No parentheses present
"" 0 Empty string returns 0
"(()(()))" 3 Nested parentheses with maximum 3 levels
(a+(b*(c+(d)))) 4 Characters ignored, structure leads to depth 4

Visualization Player

Solution

To find the maximum nesting depth of parentheses in a string, we need to understand what 'nesting' really means. It simply refers to how many parentheses are open at the same time at any point in the string.

Let's say we see an opening bracket ( — we increase our current depth by 1. When we see a closing bracket ), we decrease the depth by 1. The maximum value that the current depth ever reaches is the maximum nesting depth.

For example, in the string (1+(2*3)+((8)/4))+1, when we reach ((8)/4), we have three ( brackets opened before any are closed. So, the nesting depth there is 3, and that’s the deepest it ever gets.

If the string is something simple like 1+2 or abc, there are no brackets, so the nesting depth is just 0.

Even in more complex-looking strings like (a+(b*(c+(d)))), we ignore all characters except the brackets. The string reaches a depth of 4 due to four levels of parentheses before they start closing.

If the string is empty, or contains only characters without any ( or ), then the answer is also 0 — there's no nesting at all.

What's great about this approach is that we don't need a stack. We just track how many parentheses are open as we scan left to right, and keep updating the maximum depth reached. It’s very efficient and runs in O(n) time.

Algorithm Steps

  1. Initialize two variables: depth = 0 and maxDepth = 0.
  2. Loop through each character ch in the string s.
  3. If ch == '(', increment depth and update maxDepth if depth > maxDepth.
  4. If ch == ')', decrement depth.
  5. Return maxDepth at the end.

Code

Python
Java
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
def max_depth(s):
    depth = 0
    max_depth = 0
    for ch in s:
        if ch == '(':
            depth += 1               # Increase current depth
            max_depth = max(max_depth, depth)  # Update max if needed
        elif ch == ')':
            depth -= 1               # Decrease depth on closing bracket
    return max_depth

# Sample input
print(max_depth("(1+(2*3)+((8)/4))+1"))  # Output: 3

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n)We must scan every character once, even if there are few or no parentheses.
Average CaseO(n)Each character is checked, and counters are updated accordingly.
Worst CaseO(n)Even with maximum depth and complexity, each character is visited once.

Space Complexity

O(1)

Explanation: Only a few counters are used. No extra data structures like stacks are required.