Measure the Deepest Parenthesis Nest. The parentheses in s are balanced. Other characters are digits or arithmetic operator symbols. Return the largest number of simultaneously open parentheses while reading the string.
### Problem
The parentheses in `s` are balanced. Other characters are digits or arithmetic operator symbols. Return the largest number of simultaneously open parentheses while reading the string.
```cpp
class Solution {
public:
int maxDepth(string s);
};
```
### Examples
**Example 1:**
```text
s = "2*((3+4)/(5-1))"
Output: 2
Explanation: The groups around 3+4 and 5-1 are each nested inside the outer multiplication group.
```
**Example 2:**
```text
s = "7+8/2"
Output: 0
Explanation: No parentheses occur.
```
### Constraints
- `1 <= s.length <= 100`.
- Permitted characters are digits `0..9` and `+`, `-`, `*`, `/`, `(`, `)`.
- The parentheses form a valid balanced sequence.
### Clarifying Questions
Do not evaluate the arithmetic expression. Parenthesis validity is guaranteed rather than something the function must reject.
```hint Inspect the structure
How do nested groups differ from consecutive groups in the requested depth?
```
### Reasoning checkpoints
- Relate the reported nesting level to the balanced parentheses without evaluating the arithmetic expression.
- Distinguish nested groups from adjacent groups and handle a string with no parentheses.
### Optional extensions
What additional check would detect an invalid parenthesis sequence?
Overview: Measure the Deepest Parenthesis Nest. The parentheses in s are balanced. Other characters are digits or arithmetic operator symbols. Return the largest number of simultaneously open parentheses while reading the string.
The parentheses in s are balanced. Other characters are digits or arithmetic operator symbols. Return the largest number of simultaneously open parentheses while reading the string.
class Solution {
public:
int maxDepth(string s);
};
Examples
Example 1:
s = "2*((3+4)/(5-1))"
Output: 2
Explanation: The groups around 3+4 and 5-1 are each nested inside the outer multiplication group.
Example 2:
s = "7+8/2"
Output: 0
Explanation: No parentheses occur.
Constraints
1 <= s.length <= 100
.
Permitted characters are digits
0..9
and
+
,
-
,
*
,
/
,
(
,
)
.
The parentheses form a valid balanced sequence.
Clarifying Questions Guidance
Do not evaluate the arithmetic expression. Parenthesis validity is guaranteed rather than something the function must reject.
Reasoning checkpoints
Relate the reported nesting level to the balanced parentheses without evaluating the arithmetic expression.
Distinguish nested groups from adjacent groups and handle a string with no parentheses.
Optional extensions
What additional check would detect an invalid parenthesis sequence?