File: maximum-nesting-depth-of-the-parentheses/solution.py

Date: 2026-06-06

Time: 17:38

Purpose

This file solves LeetCode 1614: Maximum Nesting Depth of the Parentheses. It determines the deepest level of nested parentheses in a valid parentheses string (VPS). The string may contain non-parenthesis characters, which are ignored.

Key Components

Solution.maxDepth(self, s: str) -> int

Takes a VPS string and returns the maximum nesting depth. Uses two integer accumulators:

Patterns

Single-pass counter pattern. This is the canonical approach for parenthesis depth problems: maintain a running depth counter and track its peak. No stack is needed because the problem only asks for the *depth*, not the structure. This reduces space from O(n) (stack-based) to O(1).

The comparison if depth > maxdepth is used instead of maxdepth = max(max_depth, depth), avoiding a function call per character. Minor, but consistent with a pattern seen across this repo's solutions of preferring inline comparisons over max()/min() calls in tight loops.

Dependencies

Imports: None. Pure self-contained solution with no standard library or external dependencies.

Imported by: maximum-nesting-depth-of-the-parentheses/test_solution.py directly. The "Imported By" list in the prompt is misleading — those are all test files across the repo that share a common test harness import pattern, not actual consumers of this solution's logic.

Flow

1. Initialize depth and max_depth to 0.

2. Iterate character by character through s.

3. On (: increment depth, then check if it's a new maximum.

4. On ): decrement depth.

5. All other characters are ignored (the for loop simply skips them).

6. Return max_depth after the full scan.

For input "(1+(2*3)+((8)/4))+1":


(  →  depth=1, max=1
(  →  depth=2, max=2
)  →  depth=1
(  →  depth=2
(  →  depth=3, max=3
)  →  depth=2
)  →  depth=1
)  →  depth=0
→ returns 3

Invariants

Error Handling

None. The method assumes valid input per the problem constraints. No exceptions are raised, no edge cases are guarded. An empty string returns 0 (correct), and a string with no parentheses also returns 0 (correct).

Topics to Explore

Beliefs