424. Longest Repeating Character Replacement

Given a string `s` that consists of only uppercase English letters, you can perform at most `k` operations on that string.

In one operation, you can choose any character of the string and change it to any other uppercase English character.

Find the length of the longest sub-string containing all repeating letters you can get after performing the above operations.

Note:
Both the string's length and k will not exceed 104.

Example 1:

```Input:
s = "ABAB", k = 2

Output:
4

Explanation:
Replace the two 'A's with two 'B's or vice versa.
```

Example 2:

```Input:
s = "AABABBA", k = 1

Output:
4

Explanation:
Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
```

424. Longest Repeating Character Replacement
``````struct Solution;

impl Solution {
fn character_replacement(s: String, k: i32) -> i32 {
let mut count = vec![0; 26];
let n = s.len();
let s: Vec<char> = s.chars().collect();
let mut start = 0;
let mut end = 0;
let mut res = 0;
while end < n {
if Self::sum(&count) <= k {
count[(s[end] as u8 - b'A') as usize] += 1;
end += 1;
} else {
count[(s[start] as u8 - b'A') as usize] -= 1;
start += 1;
}
if Self::sum(&count) <= k {
res = res.max(end - start);
}
}
res as i32
}

fn sum(count: &[i32]) -> i32 {
let max = count.iter().copied().max().unwrap();
count.iter().sum::<i32>() - max
}
}

#[test]
fn test() {
let s = "ABAB".to_string();
let k = 2;
let res = 4;
assert_eq!(Solution::character_replacement(s, k), res);
let s = "AABABBA".to_string();
let k = 1;
let res = 4;
assert_eq!(Solution::character_replacement(s, k), res);
}
``````