use std::collections::HashMap;
impl Solution {
pub fn max_freq(s: String, max_letters: i32, min_size: i32, max_size: i32) -> i32 {
let cs = s.as_bytes();
let mut map = HashMap::new();
let mut win = HashMap::new();
for i in 0..min_size as usize {
win.insert(cs[i], i);
}
if win.len() <= max_letters as usize {
map.insert(&s[0..min_size as usize], 1);
}
for i in min_size as usize..cs.len() {
if let Some(start) = win.get(&cs[i - min_size as usize]) {
if start <= &(i - min_size as usize) {
win.remove(&cs[i - min_size as usize]);
}
}
win.insert(cs[i], i);
if win.len() <= max_letters as usize {
if let Some(count) = map.get_mut(&s[i - min_size as usize + 1 .. i + 1]) {
*count += 1;
} else {
map.insert(&s[i - min_size as usize + 1 .. i + 1], 1);
}
}
}
let mut ans = 0;
for (_, v) in &map {
ans = ans.max(*v);
}
return ans;
}
}