LeetCode 1297. 子串的最大出现次数
原题链接
中等
作者:
autumn_0
,
2024-10-08 08:43:12
,
所有人可见
,
阅读 4
class Solution {
public int maxFreq(String S, int maxLetters, int minSize, int maxSize) {
char[] s = S.toCharArray();
var cnt = new HashMap<Character, Integer>();
var res = new HashMap<String, Integer>();
for(int i = 0; i < minSize - 1; i ++ ) cnt.merge(s[i], 1, Integer::sum);
for(int i = minSize - 1; i < s.length; i ++ ){
cnt.merge(s[i], 1, Integer::sum);
if(cnt.size() <= maxLetters) {
res.merge(S.substring(i - minSize + 1 ,i + 1), 1, Integer::sum);
}
char out = s[i - minSize + 1];
if(cnt.merge(out, -1, Integer::sum) == 0)
cnt.remove(out);
}
int ans = 0;
for(Integer value: res.values()){
ans = Math.max(ans, value);
}
return ans;
}
}