forked from LeetCode-in-Rust/LeetCode-in-Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.rs
52 lines (44 loc) · 1.21 KB
/
Solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1)
// #2024_09_05_Time_0_ms_(100.00%)_Space_2.3_MB_(61.46%)
impl Solution {
pub fn longest_valid_parentheses(s: String) -> i32 {
let mut max_len = 0;
let mut left = 0;
let mut right = 0;
let n = s.len();
let chars: Vec<char> = s.chars().collect();
// Left to right pass
for i in 0..n {
if chars[i] == '(' {
left += 1;
} else {
right += 1;
}
if right > left {
left = 0;
right = 0;
}
if left == right {
max_len = max_len.max(left + right);
}
}
// Right to left pass
left = 0;
right = 0;
for i in (0..n).rev() {
if chars[i] == '(' {
left += 1;
} else {
right += 1;
}
if left > right {
left = 0;
right = 0;
}
if left == right {
max_len = max_len.max(left + right);
}
}
max_len as i32
}
}