//循环26次,每次次限定符合条件的不同字符有1,2,3.....26个
class Solution {
public int longestSubstring(String s, int k) {
int res = 0;
for(int i = 1; i <= 26; i++){
res = Math.max(res, helper(s, k, i));
}
return res;
}
private int helper(String s, int k, int i){
int[] map = new int[26];
int l = 0;
int r = 0;
int res = Integer.MIN_VALUE;
int cnt = 0;
int numNoLessThanK = 0;
while(r < s.length()){
int cl = s.charAt(r) - 'a';
if(map[cl]++ == 0){
cnt++;
}
if(map[cl] == k){
numNoLessThanK++;
}
r++;
while(cnt > i){
int c2 = s.charAt(l) - 'a';
if(map[c2]-- == k){
numNoLessThanK--;
}
if(map[c2] == 0){
cnt--;
}
l++;
}
if(cnt == numNoLessThanK){
res = Math.max(res, r - l);
}
}
return res;
}
}
09-10
09-10