Description
Maximum Nesting Depth of the Parentheses
EasyGiven 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 <= 100sconsists of digits0-9and characters'+','-','*','/','(', and')'.- It is guaranteed that parentheses expression
sis a VPS.
Solutions
Attempt at better performance.
Language: javascript()DONE
CPU Performance40.94%
x
Memory Performance34.65%
x
/**
* @param {string} s
* @return {number}
*/
var maxDepth = function(s) {
let paren = 0;
let max = 0;
for (const char of s) {
if (char === '(') {
paren++;
} else if (char === ')') {
max = Math.max(max, paren);
paren--;
}
}
return max;
};