Skip to content

Commit c3e2ff6

Browse files
authored
Merge pull request #1305 from zim0369/424-longest-repeating-character-replacement
Create 424-Longest-Repeating-Character-Replacement.rs
2 parents 588d842 + 692fa20 commit c3e2ff6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn character_replacement(s: String, k: i32) -> i32 {
5+
let s: Vec<char> = s.chars().collect();
6+
let (mut res, mut l, mut maxf) = (0, 0, 0);
7+
let mut count: HashMap<char, u64> = HashMap::new();
8+
9+
for r in 0..s.len() {
10+
*count.entry(s[r]).or_default() += 1;
11+
maxf = maxf.max(*count.get(&s[r]).unwrap());
12+
13+
while (r - l + 1) - maxf as usize > k as usize {
14+
*count.get_mut(&s[l]).unwrap() -= 1;
15+
l += 1;
16+
}
17+
18+
res = res.max(r - l + 1);
19+
}
20+
21+
res as i32
22+
}
23+
}
24+

0 commit comments

Comments
 (0)