Given a valid parentheses string s, return its maximum nesting depth. The depth at any point is the number of opening parentheses that have yet to be closed.
The parentheses in s are balanced and correctly nested. Digits and arithmetic operators have no effect on the depth.
Example 1
Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3
Explanation: When the scan reaches 8, three pairs of parentheses are open. Two belong to ((8)/4), and the third is the outer pair around 1+(2*3)+((8)/4).
Example 2
Input: s = "(1)+((2))+(((3)))"
Output: 3
Explanation: The groups around 1, 2, and 3 reach depths 1, 2, and 3, respectively. The maximum depth is 3.
Brute Force Approach
Use a stack to keep track of opening parentheses that have not yet been closed. Each time we encounter (, push it onto the stack. This means we have entered another level of nesting.
When we encounter ), it closes the most recent unmatched (, so we pop the top of the stack. Digits and operators do not affect the nesting depth, so we skip them.
The stack size tells us how many pairs of parentheses are currently open. For example, after reading (((, the stack contains three opening parentheses, so the current depth is 3. The largest stack size reached during the scan is the maximum nesting depth.
Approach
Initialise an empty stack and set the maximum depth to
0.Traverse the string one character at a time.
If the character is
(, push it onto the stack and update the maximum depth if the stack size is larger.If the character is
), pop the matching opening parenthesis from the stack.Skip all other characters because they do not affect the depth.
Return the maximum depth after scanning the entire string.
Dry Run
Maximum Nesting Depth of Parentheses Brute force
Solution
#include <bits/stdc++.h>using namespace std;class Solution {public: // Find the maximum nesting depth in a string with balanced parentheses. int maxDepth(const string& s) { stack<char> openParentheses; int deepest = 0; for (char ch : s) { if (ch == '(') { // The stack size gives the current nesting depth. openParentheses.push(ch); int currentDepth = openParentheses.size(); deepest = max(deepest, currentDepth); } else if (ch == ')') { // Remove the most recent unmatched opening parenthesis. openParentheses.pop(); } } return deepest; }};int main() { Solution solution; cout << solution.maxDepth("(1+(2*3)+((8)/4))+1") << '\n'; // Prints 3. return 0;}Complexity Analysis
Time Complexity : O(N), where N is the string length. Each character is scanned once, and each stack operation takes constant time.
Space Complexity : O(D) auxiliary space, where D is the maximum nesting depth. In the worst case, D grows linearly with N.
Optimal Approach
The current nesting depth depends on how many opening parentheses have not yet been closed. Since we only need this count, we can track it using an integer called depth.
Each time we encounter (, increment depth because we are entering another level of nesting. When we encounter ), decrement it because one level has closed. Digits and operators do not affect the depth, so we skip them.
Keep another variable, deepest, to store the largest depth reached during the scan. For example, reading ((( increases depth to 3. Closing those parentheses reduces the current depth, while deepest remains 3.
Approach
Initialise
depthanddeepestto0.Traverse the string one character at a time.
If the character is
(, incrementdepthand updatedeepestif the current depth is larger.If the character is
), decrementdepth.Skip all other characters because they do not affect the depth.
Return
deepestafter scanning the entire string.
Dry Run
Maximum Nesting Depth of Parentheses Optimal
Solution
#include <bits/stdc++.h>using namespace std;class Solution {public: // Find the maximum nesting depth in a string with balanced parentheses. int maxDepth(const string& s) { int depth = 0; int deepest = 0; // Only parentheses affect the depth. for (char ch : s) { if (ch == '(') { // An opening parenthesis adds one level of nesting. ++depth; deepest = max(deepest, depth); } else if (ch == ')') { // A closing parenthesis ends one level of nesting. --depth; } } return deepest; }};int main() { Solution solution; cout << solution.maxDepth("(1+(2*3)+((8)/4))+1") << '\n'; // Prints 3. return 0;}Complexity Analysis
Time Complexity : O(N) because the algorithm scans the string once.
Space Complexity : O(1) auxiliary space. The solution uses two counters and reads the input by reference.
Interview follow-up Questions
The answer is 0. For example, in "1+2*3", the depth stays at 0 throughout the scan.
Be the first to add a comment.