395. Longest Substring with At least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.
Example 1:
Input:
s = "aaabb", k = 3
Output:
3
The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input:
s = "ababbc", k = 2
Output:
5
The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
算法分析
先统计出字符串中每个字符出现的次数,对于出现次数少于k的字符,任何一个包含该字符的字符串都不是符合要求的子串,因此这样的字符就是分隔符,应该以这些出现次数少于k次的字符做分隔符打断原字符串,然后对各个打断得到的字符串进行递归统计,得到最长的符合要求的字符串。如果一个字符串中不包含分隔符(即每个字符出现的次数都达到了k次及以上次数),那么这个字符串就是符合要求的子串。
Java 算法实现:
public class Solution {
public int longestSubstring(String s, int k) {
if(k<=1){
return s.length();
}
int[] repeat=new int['z'+1];
for(int i=0;i<s.length();i++){
repeat[s.charAt(i)]++;
}
StringBuilder regex=new StringBuilder("");
boolean firstSplit=true;
for(int i='a';i<='z';i++){
if(repeat[i]>0&&repeat[i]<k){
if(firstSplit){
regex.append((char)i);
firstSplit=false;
}
else{
regex.append("|"+(char)i);
}
}
}
if(regex.length()>0){
//说明有分隔符
String[] strs=s.split(regex.toString());
int max=0;
int tmpAns=0;
for(String str:strs){
tmpAns=longestSubstring(str, k);
if(tmpAns>max){
max=tmpAns;
}
}
return max;
}
else{
//没有分隔符,说明s中的每一个字符出现的次数都大于等于k
return s.length();
}
}
}