Maximum Nesting Depth of the Parentheses (LeetCode 1614): Solution and Explanation
Given a valid parentheses expression that may also contain digits and + - * /, return its nesting depth: the largest number of parentheses open at the same moment.
Intuition
A stack-based approach works: push on every (, pop on every ), and record the largest stack size. But the stack only ever holds ( characters, so its contents carry no information beyond its size.
Replace the stack with a counter. At any point while scanning, the counter equals the number of parentheses that are open, which is exactly how deeply the current character is nested.
Picture the counter as a skyline drawn over the string. Each ( raises it by one, each ) lowers it by one, and digits and operators leave it flat. The answer is the height of the tallest point.
The input is guaranteed valid, so the counter never goes negative and there is no need to check matching.
Approach: Depth counter
- Set
depth = 0andbest = 0. - For each
(, incrementdepthand setbest = max(best, depth). - For each
), decrementdepth. - Skip every other character. Return
best.
Step-by-step example
s = "(1+(2*3)+((8)/4))+1"
- Index 0 '(' raises depth to 1. best = 1.
- "1+" changes nothing. Index 3 '(' raises depth to 2, best = 2.
- "2*3" is flat. Index 7 ')' lowers depth to 1.
- "+" is flat. Index 9 '(' raises depth to 2, then index 10 '(' raises it to 3: best = 3.
- "8" sits at depth 3. The remaining ')' characters bring depth back to 0, and nothing goes above 3.
- Return 3.
Code: Python, Java, C++
1class Solution:
2 def maxDepth(self, s: str) -> int:
3 depth = 0
4 best = 0
5 for ch in s:
6 if ch == '(':
7 depth += 1
8 best = max(best, depth)
9 elif ch == ')':
10 depth -= 1
11 return best1class Solution {
2 public int maxDepth(String s) {
3 int depth = 0, best = 0;
4 for (char ch : s.toCharArray()) {
5 if (ch == '(') {
6 depth++;
7 best = Math.max(best, depth);
8 } else if (ch == ')') {
9 depth--;
10 }
11 }
12 return best;
13 }
14}1class Solution {
2public:
3 int maxDepth(string s) {
4 int depth = 0, best = 0;
5 for (char ch : s) {
6 if (ch == '(') {
7 depth++;
8 best = max(best, depth);
9 } else if (ch == ')') {
10 depth--;
11 }
12 }
13 return best;
14 }
15};Time and space complexity
O(n)
Each character is examined once with constant work.
O(1)
Only two integers are kept, whatever the length of the string.
Other approaches
Explicit stack (time O(n), space O(n))
Push on (, pop on ), and track the maximum stack size. It gives the same answer and generalises to multiple bracket types, but uses O(n) memory for no benefit here.
Common mistakes
- Updating
beston)instead of(. The peak is reached right after an opening parenthesis, so checking only on close works too but is easy to get off by one. - Counting the total number of
(characters instead of how many are open at once.()()()has three pairs but depth 1. - Treating digits or operators as if they change the depth.
- Returning the final depth instead of the maximum. For a valid string the final depth is always 0.
Frequently asked questions
Why is a counter enough instead of a stack?
There is only one bracket type, so the stack holds nothing but ( characters and only its size matters. An integer tracks that size directly.
What if the string has no parentheses?
Nothing is ever nested, so the depth is 0. The counter never moves and best stays at 0.
Do the digits and operators matter?
No. They never change the depth, so the algorithm skips them. They only exist to make the string look like an arithmetic expression.
What is the time complexity of Maximum Nesting Depth of the Parentheses?
O(n) time and O(1) extra space, where n is the length of the string.