只能说还是太菜,抄的网上大神的做法:
idea:
代码如下:
public int longestSubstring(String s, int k) {
if (k == 1) return s.length();
int res = 0, i = 0, n = s.length();
while (i + k <= n){
int[] m = new int[26];
int mask = 0, max_idx = i;
for (int j = i; j < n; j ++){
int t = s.charAt(j) - 'a';
m[t]++;
if (m[t] < k) mask |= (1 << t);
else mask &= (~(1 << t));
if (mask == 0){
res = Math.max(res, j - i + 1);
max_idx = j;
}
}
i = max_idx + 1;
}
return res;
}
还有滑动窗口的做法,接下来更新。
leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)
原文:https://www.cnblogs.com/whyaza/p/10779039.html