class Solution:
def maxDepth(self, s: str) -> int:
max_depth = 0
current_depth = 0
for char in s:
if char == '(':
current_depth += 1
max_depth = max(max_depth, current_depth)
elif char == ')':
current_depth -= 1
return max_depth
class Solution {
public:
int maxDepth(string s) {
int max_depth = 0, current_depth = 0;
for (char c : s) {
if (c == '(') {
current_depth++;
max_depth = max(max_depth, current_depth);
} else if (c == ')') {
current_depth--;
}
}
return max_depth;
}
};
class Solution {
public int maxDepth(String s) {
int maxDepth = 0, currentDepth = 0;
for (char c : s.toCharArray()) {
if (c == '(') {
currentDepth++;
maxDepth = Math.max(maxDepth, currentDepth);
} else if (c == ')') {
currentDepth--;
}
}
return maxDepth;
}
}
var maxDepth = function(s) {
let maxDepth = 0, currentDepth = 0;
for (let char of s) {
if (char === '(') {
currentDepth++;
maxDepth = Math.max(maxDepth, currentDepth);
} else if (char === ')') {
currentDepth--;
}
}
return maxDepth;
};
Given a string s
consisting of digits, operators, and parentheses, you are asked to determine the maximum nesting depth of the parentheses in s
. The nesting depth is defined as the maximum number of parentheses that are open at any point in the string.
For example, in the string "(1+(2*3)+((8)/4))+1"
, the maximum nesting depth is 3
because at one point there are three open parentheses before any are closed.
Key constraints:
s
is a valid parentheses string (VPS), meaning all open parentheses have a matching closing parenthesis and are properly nested.
The core of this problem is to track how deeply nested the parentheses are as we read through the string. At each character, if we see an open parenthesis '('
, we know we are going one level deeper. If we see a close parenthesis ')'
, we move one level up (out of a nested level).
The simplest approach is to scan the string from left to right, keeping a counter for the current nesting level. Each time we see an open parenthesis, we increment the counter; each time we see a close parenthesis, we decrement it. We keep track of the maximum value this counter reaches, which will be our answer.
A brute-force approach might try to check every possible substring, but that's unnecessary since the nesting depth is determined by the sequence of open and close parentheses, not by their specific positions or contents. Recognizing this allows us to optimize to a single pass with constant space.
We can solve this problem efficiently in one pass through the string using a simple counter. Here’s how:
current_depth
(to track the current level of nesting) and max_depth
(to record the maximum depth seen so far).s
:
'('
, increment current_depth
by 1. Then, update max_depth
if current_depth
is greater than max_depth
.')'
, decrement current_depth
by 1.max_depth
will contain the answer.This approach is efficient because:
Let's use the input s = "(1+(2*3)+((8)/4))+1"
.
current_depth = 0
, max_depth = 0
.'('
: current_depth = 1
, max_depth = 1
.'('
: current_depth = 2
, max_depth = 2
.'('
: current_depth = 3
, max_depth = 3
.')'
: current_depth = 2
.current_depth
returns to 0.max_depth
always holds the highest value current_depth
reached, which is 3.
The function returns 3
, which is the correct answer.
Brute-force approach:
This makes the optimized approach highly efficient for even very large strings.
To solve the Maximum Nesting Depth of Parentheses problem, we track the current depth as we scan the string and record the maximum value reached. This elegant approach leverages the fact that parentheses are properly nested and does not require extra data structures or checking substrings. The result is a simple, fast, and space-efficient solution that works for any valid input.