给你一个字符串 s
和一个整数 k
。你可以选择字符串中的任一字符,并将其更改为任何其他大写英文字符。该操作最多可执行 k
次。
在执行上述操作后,返回 包含相同字母的最长子字符串的长度。
输入: s = "ABAB", k = 2 输出: 4 解释: 用两个'A'替换为两个'B',反之亦然。
输入: s = "AABABBA", k = 1 输出: 4 解释: 将中间的一个'A'替换为'B',字符串变为 "AABBBBA"。 子串 "BBBB" 有最长重复字母, 答案为 4。 可能存在其他的方法来得到同样的结果。
1 <= s.length <= 105
s
仅由大写英文字母组成0 <= k <= s.length
impl Solution {
pub fn character_replacement(s: String, k: i32) -> i32 {
let s = s.as_bytes();
let mut count = [0; 26];
let mut most_ch = b'A';
let mut i = -1;
let mut ret = 0;
for j in 0..s.len() {
count[(s[j] - b'A') as usize] += 1;
if count[(s[j] - b'A') as usize] > count[(most_ch - b'A') as usize] {
most_ch = s[j];
}
while j as i32 - i - count[(most_ch - b'A') as usize] > k {
i += 1;
count[(s[i as usize] - b'A') as usize] -= 1;
if s[i as usize] == most_ch {
most_ch = (0..26).max_by_key(|&i| count[i]).unwrap() as u8 + b'A';
}
}
ret = ret.max(j as i32 - i);
}
ret
}
}