1614. Maximum Nesting Depth of the Parentheses - Explanation
Description
You are 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: 3Explanation: Digit 8 is inside of 3 nested parentheses in the string.
Example 2:
Input: s = "(1)+((2))+(((3)))"
Output: 3Explanation: Digit 3 is inside of 3 nested parentheses in the string.
Example 3:
Input: s = "()(())((()()))"
Output: 3Constraints:
1 <= s.length <= 100sconsists of digits0-9and characters'+','-','*','/','(', and')'.- It is guaranteed that parentheses expression
sis a VPS.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- String Traversal - Iterating through characters in a string one by one
- Stack Data Structure - Understanding how stacks work for tracking nested/paired elements
- Recursion Basics - Using recursive function calls to process sequential data
1. Recursion
Intuition
We can process the string from right to left using recursion. Each open parenthesis increases our depth count, and each close parenthesis decreases it. By tracking the maximum absolute value of this count at any point, we find the deepest nesting level. Processing from right to left means we encounter closing parentheses first, which decrement the counter, and opening parentheses later, which increment it.
Algorithm
- Initialize a result variable
resto track the maximum depth. - Use recursion starting from index
0. At each step, first recurse to the next index to get the running count. - If the current character is
(, increment the count. If it's), decrement the count. - Update
reswith the maximum ofresand the absolute value of the count. - Return the final result after processing all characters.
Time & Space Complexity
- Time complexity:
- Space complexity: for recursion stack.
2. Stack
Intuition
A stack naturally models nested structures. Each time we see an opening parenthesis, we push it onto the stack, increasing the current depth. Each closing parenthesis pops from the stack, decreasing the depth. The maximum stack size during traversal equals the maximum nesting depth.
Algorithm
- Initialize an empty stack and a result variable
res = 0. - Iterate through each character in the string.
- If the character is
(, push it onto the stack and updatereswith the maximum ofresand the current stack size. - If the character is
), pop from the stack. - Return
resafter processing all characters.
Time & Space Complexity
- Time complexity:
- Space complexity:
3. Iteration
Intuition
We don't actually need to store the parentheses in a stack. Since we only care about the depth (stack size), we can replace the stack with a simple counter. This reduces space complexity to O(1) while maintaining the same logic.
Algorithm
- Initialize
res = 0to track the maximum depth andcur = 0to track the current depth. - Iterate through each character in the string.
- If the character is
(, incrementcur. If it's), decrementcur. - After each character, update
reswith the maximum ofresandcur. - Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity: extra space.
Common Pitfalls
Counting All Characters
A frequent mistake is incrementing or decrementing the depth counter for every character in the string. The string may contain digits, operators, and other characters that should be ignored. Only parentheses ( and ) affect the nesting depth.
Updating Maximum at the Wrong Time
When using a counter approach, the maximum depth should be updated after incrementing for (, not after decrementing for ). Updating after processing ) will miss the peak depth that occurred when the matching ( was processed.
Sign in to join the discussion