Skip to content

Commit 7d2463d

Browse files
committed
1614. Maximum Nesting Depth of the Parentheses: AC
1 parent beb7554 commit 7d2463d

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1216,3 +1216,4 @@ mod s1608_special_array_with_x_elements_greater_than_or_equal_x;
12161216
mod s1609_even_odd_tree;
12171217
mod s1610_maximum_number_of_visible_points;
12181218
mod s1611_minimum_one_bit_operations_to_make_integers_zero;
1219+
mod s1614_maximum_nesting_depth_of_the_parentheses;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* [1614] Maximum Nesting Depth of the Parentheses
3+
*
4+
* Given a valid parentheses string s, return the nesting depth of s. The nesting depth is the maximum number of nested parentheses.
5+
*
6+
* Example 1:
7+
* <div class="example-block">
8+
* Input: <span class="example-io">s = "(1+(2*3)+((8)/4))+1"</span>
9+
* Output: <span class="example-io">3</span>
10+
* Explanation:
11+
* Digit 8 is inside of 3 nested parentheses in the string.
12+
* </div>
13+
* Example 2:
14+
* <div class="example-block">
15+
* Input: <span class="example-io">s = "(1)+((2))+(((3)))"</span>
16+
* Output: <span class="example-io">3</span>
17+
* Explanation:
18+
* Digit 3 is inside of 3 nested parentheses in the string.
19+
* </div>
20+
* Example 3:
21+
* <div class="example-block">
22+
* Input: <span class="example-io">s = "()(())((()()))"</span>
23+
* Output: <span class="example-io">3</span>
24+
* </div>
25+
*
26+
* Constraints:
27+
*
28+
* 1 <= s.length <= 100
29+
* s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.
30+
* It is guaranteed that parentheses expression s is a VPS.
31+
*
32+
*/
33+
pub struct Solution {}
34+
35+
// problem: https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/
36+
// discuss: https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/discuss/?currentPage=1&orderBy=most_votes&query=
37+
38+
// submission codes start here
39+
40+
impl Solution {
41+
pub fn max_depth(s: String) -> i32 {
42+
s.chars()
43+
.scan(0, |mut state, c| {
44+
match c {
45+
'(' => *state += 1,
46+
')' => *state -= 1,
47+
_ => (),
48+
}
49+
Some(*state)
50+
})
51+
.max()
52+
.unwrap_or(0)
53+
}
54+
}
55+
56+
// submission codes end
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
62+
#[test]
63+
fn test_1614_example_1() {
64+
let s = "(1+(2*3)+((8)/4))+1".to_string();
65+
66+
let result = 3;
67+
68+
assert_eq!(Solution::max_depth(s), result);
69+
}
70+
71+
#[test]
72+
fn test_1614_example_2() {
73+
let s = "(1)+((2))+(((3)))".to_string();
74+
75+
let result = 3;
76+
77+
assert_eq!(Solution::max_depth(s), result);
78+
}
79+
80+
#[test]
81+
fn test_1614_example_3() {
82+
let s = "()(())((()()))".to_string();
83+
84+
let result = 3;
85+
86+
assert_eq!(Solution::max_depth(s), result);
87+
}
88+
}

0 commit comments

Comments
 (0)