8000 Create December-10.rs · Hunterdii/Leetcode-POTD@5bc024e · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bc024e

Browse files
authored
Create December-10.rs
1 parent 2056635 commit 5bc024e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn maximum_length(s: String) -> i32 {
3+
fn is_valid(s: &str, n: usize, x: usize) -> bool {
4+
let mut freq = vec![0; 26];
5+
let bytes = s.as_bytes();
6+
let mut i = 0;
7+
while i < n {
8+
let mut j = i + 1;
9+
while j < n && bytes[j] == bytes[i] {
10+
j += 1;
11+
}
12+
let index = (bytes[i] - b'a') as usize;
13+
freq[index] += (j - i).saturating_sub(x - 1);
14+
if freq[index] >= 3 {
15+
return true;
16+
}
17+
i = j;
18+
}
19+
false
20+
}
21+
22+
let n = s.len();
23+
let mut l = 0;
24+
let mut r = n;
25+
while l < r {
26+
let mid = (l + r + 1) / 2;
27+
if is_valid(&s, n, mid) {
28+
l = mid;
29+
} else {
30+
r = mid - 1;
31+
}
32+
}
33+
if l == 0 { -1 } else { l as i32 }
34+
}
35+
}

0 commit comments

Comments
 (0)
0