Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1305 from zim0369/424-longest-repeatin…
Browse files Browse the repository at this point in the history
…g-character-replacement

Create 424-Longest-Repeating-Character-Replacement.rs
  • Loading branch information
Ahmad-A0 authored Oct 22, 2022
2 parents 588d842 + 692fa20 commit c3e2ff6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rust/424-Longest-Repeating-Character-Replacement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::collections::HashMap;

impl Solution {
pub fn character_replacement(s: String, k: i32) -> i32 {
let s: Vec<char> = s.chars().collect();
let (mut res, mut l, mut maxf) = (0, 0, 0);
let mut count: HashMap<char, u64> = HashMap::new();

for r in 0..s.len() {
*count.entry(s[r]).or_default() += 1;
maxf = maxf.max(*count.get(&s[r]).unwrap());

while (r - l + 1) - maxf as usize > k as usize {
*count.get_mut(&s[l]).unwrap() -= 1;
l += 1;
}

res = res.max(r - l + 1);
}

res as i32
}
}

0 comments on commit c3e2ff6

Please sign in to comment.