tutorialcup
Maximum Nesting Depth of the Parentheses Leetcode Solution
Problem Statement
In this problem, we are given a valid parentheses string (vps) having some numbers, some operators(e.g. +,-,*) and some parentheses(e.g. '(',')').
Valid parentheses strings (vps) are:
- ""
- "d" where d is any number
- "(A)" if A is valid parentheses string
- "A*B" if * is any operator and A and B are valid parentheses string.
Our aim is to find the maximum depth of the parentheses in the given string.
e.g. "(1+(2*3)+((8)/4))+1"
we can see that at the number 8 is inside the maximum number of parentheses and the depth of parentheses there is 3. We will output 3.
Let's see one more example,
e.g. "1+(2*3)/(2-1)"
the vps 2*3 or another vps 2-1, both are inside the maximum depth of parentheses i.e. 1.
Example
s = "(1+(2*3)+((8)/4))+1"
3
Explanation:
Digit 8 is inside of 3 nested parentheses in the string.
s = "(1)+((2))+(((3)))"
3
Approach
We are given that the string is a vps and we just have to find out the maximum depth of parentheses. So, we can just focus on the parentheses and ignore other characters (numbers and operators).
The depth of parentheses increase only when there starts a new nested parentheses. i.e. the starting parentheses '(' means that depth of parentheses increase by 1. And when the parentheses is closed, then depth decreases by 1. i.e. ')' means depth is decreased by 1.
In our approach we will just use a current depth variable (let k) and a maximum depth variable (let ans).Both
www.tutorialcup.com/leetcode-solutions/maximum-nesting-de...
Maximum Nesting Depth of the Parentheses Leetcode Solution
Problem Statement
In this problem, we are given a valid parentheses string (vps) having some numbers, some operators(e.g. +,-,*) and some parentheses(e.g. '(',')').
Valid parentheses strings (vps) are:
- ""
- "d" where d is any number
- "(A)" if A is valid parentheses string
- "A*B" if * is any operator and A and B are valid parentheses string.
Our aim is to find the maximum depth of the parentheses in the given string.
e.g. "(1+(2*3)+((8)/4))+1"
we can see that at the number 8 is inside the maximum number of parentheses and the depth of parentheses there is 3. We will output 3.
Let's see one more example,
e.g. "1+(2*3)/(2-1)"
the vps 2*3 or another vps 2-1, both are inside the maximum depth of parentheses i.e. 1.
Example
s = "(1+(2*3)+((8)/4))+1"
3
Explanation:
Digit 8 is inside of 3 nested parentheses in the string.
s = "(1)+((2))+(((3)))"
3
Approach
We are given that the string is a vps and we just have to find out the maximum depth of parentheses. So, we can just focus on the parentheses and ignore other characters (numbers and operators).
The depth of parentheses increase only when there starts a new nested parentheses. i.e. the starting parentheses '(' means that depth of parentheses increase by 1. And when the parentheses is closed, then depth decreases by 1. i.e. ')' means depth is decreased by 1.
In our approach we will just use a current depth variable (let k) and a maximum depth variable (let ans).Both
www.tutorialcup.com/leetcode-solutions/maximum-nesting-de...