Skip to main content

Maximum Nesting Depth of the Parentheses - Solution & Explanation

EasyStringStack16 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

Given a valid parentheses string s, return the nesting depth of s. The nesting depth is the maximum number of nested parentheses.

 

Example 1:

Input: s = "(1+(2*3)+((8)/4))+1"

Output: 3

Explanation:

Digit 8 is inside of 3 nested parentheses in the string.

Example 2:

Input: s = "(1)+((2))+(((3)))"

Output: 3

Explanation:

Digit 3 is inside of 3 nested parentheses in the string.

Example 3:

Input: s = "()(())((()()))"

Output: 3

 

Constraints:

  • 1 <= s.length <= 100
  • s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.
  • It is guaranteed that parentheses expression s is a VPS.

Approach Overview

Problem Overview: Given a valid parentheses expression mixed with characters and numbers, return the maximum nesting depth of the parentheses. Depth increases every time you enter a new ( level and decreases when encountering ). The task is to scan the string and track the deepest level reached.

Approach 1: Using a Simple Counter (Time: O(n), Space: O(1))

The simplest solution tracks the current nesting level with an integer counter. Iterate through the string character by character. When you see (, increment the counter because you are entering a deeper level; update a maxDepth variable if the current level becomes larger. When you see ), decrement the counter since that nesting level closes. All other characters are ignored. This works because valid parentheses guarantee every open parenthesis eventually closes, so the counter always represents the current active depth. The algorithm performs a single pass through the string, resulting in O(n) time complexity with constant O(1) extra space.

Approach 2: Track Depth Using Stack (Time: O(n), Space: O(n))

This method uses a stack to explicitly track open parentheses. Iterate through the string and push onto the stack whenever you encounter (. The stack size represents the current nesting depth, so update the maximum depth after each push. When encountering ), pop from the stack because that level closes. The maximum size the stack reaches during traversal equals the deepest nesting level. This approach mirrors how many parentheses validation problems are solved and can be easier to reason about if you're already thinking in terms of stack-based parsing. The traversal still runs in O(n) time, but the stack can grow to O(n) space in the worst case if the expression contains many nested parentheses.

Both approaches rely on the same insight: nesting depth equals the number of currently open parentheses. The difference is whether you track that state with a simple counter or an explicit stack structure.

Recommended for interviews: The simple counter approach is the optimal solution. It runs in O(n) time and O(1) space while remaining easy to implement and reason about. Interviewers typically expect this because the stack structure is unnecessary when only the depth is required. Showing the stack-based approach first demonstrates understanding of classic parentheses parsing with a stack, then optimizing to a counter shows strong problem-solving instincts.

Approach 1: Using a Simple Counter

This approach utilizes a single counter to track the current depth of nesting while iterating through the string. Whenever an open parenthesis '(' is encountered, the counter is incremented, and whenever a closing parenthesis ')' is encountered, the counter is decremented. The maximum value of the counter at any point during the iteration is recorded as the maximum nesting depth.

The code iterates through each character of the string. For each '(', the depth counter is increased and checked against the current maximum depth. For each ')', the depth counter is decreased. The maximum recorded depth is returned as the result.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the string. Space Complexity: O(1), as only a few variables are used.

Try this approach in the editor →

Approach 2: Track Depth Using Stack

This approach uses a stack to simulate the depth of nested parentheses. Each time an open parenthesis '(' is encountered, it is pushed onto the stack, and for each closing parenthesis ')', an item is popped. The maximum size of the stack during this process reflects the maximum nesting depth.

The C code uses an array as a stack to keep track of open parentheses. The depth is reflected by the top index of the stack, which indicates the number of open parentheses nested relatively.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n). Space Complexity: O(n) due to stack usage.

Try this approach in the editor →

Approach 3: Traversal

We use a variable d to record the current depth, initially d = 0.

Traverse the string s. When encountering a left parenthesis, increment the depth d by one and update the answer to be the maximum of the current depth d and the answer. When encountering a right parenthesis, decrement the depth d by one.

Finally, return the answer.

The time complexity is O(n), where n is the length of the string s. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

JavaScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using a Simple Counter

Time Complexity: O(n), where n is the length of the string. Space Complexity: O(1), as only a few variables are used.

Track Depth Using Stack

Time Complexity: O(n). Space Complexity: O(n) due to stack usage.

Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simple CounterO(n)O(1)Best choice when only the maximum depth is needed. Minimal memory and single pass.
Stack TrackingO(n)O(n)Useful when already solving related stack-based parentheses problems or when tracking structure explicitly.

Video Solution

maximum nesting depth of the parentheses leetcode | leetcode 1614 | string • Naresh Gupta • 14,152 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Nesting Depth of the Parentheses easy or hard?
Maximum Nesting Depth of the Parentheses is classified as an Easy problem on LeetCode. It mainly tests basic string traversal and understanding of parentheses depth tracking using a counter or stack.
Maximum Nesting Depth of the Parentheses Python/Java solution
Python and Java solutions typically iterate through the string and update a counter for '(' and ')'. The algorithm maintains a running maximum depth and finishes in O(n) time with O(1) additional space.
How to solve Maximum Nesting Depth of the Parentheses in O(n)?
Traverse the string and maintain a counter representing the current number of open parentheses. Increase it when encountering '(' and decrease it when encountering ')'. Track the maximum value reached during traversal, which represents the deepest nesting level.
What is the best approach for Maximum Nesting Depth of the Parentheses?
The best approach uses a simple counter while scanning the string once. Increment the counter for '(' and decrement for ')', while tracking the maximum value reached. This runs in O(n) time and O(1) space and avoids the overhead of a stack.
Is Maximum Nesting Depth of the Parentheses asked at Google/Amazon/Meta?
Maximum Nesting Depth of the Parentheses appears in interview preparation sets and coding practice lists used by companies like Amazon, Google, and Meta. It tests basic string processing and understanding of stack-based parentheses problems.
What data structure is used in Maximum Nesting Depth of the Parentheses?
A stack can be used to track open parentheses and measure nesting depth. However, the optimal solution replaces the stack with a simple integer counter because only the depth value is needed.
What is the time complexity of Maximum Nesting Depth of the Parentheses?
The optimal solution runs in O(n) time because the string is processed once from left to right. Each character is inspected exactly once, and updates to the depth counter or stack occur in constant time.

Ready to solve this problem?

Practice Maximum Nesting Depth of the Parentheses with our built-in code editor and test cases.

Practice on FleetCode