public class DayCode {
public static void main(String[] args) {
String s = "baaabcb";
int k = 3;
int ans = new DayCode().longestSubstring(s, k);
System.out.println("ans is " + ans);
}
/**
* https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/
* @param s
* @param k
* @return
*/
public int longestSubstring(String s, int k) {
if (s.length() < k) {
return 0;
}
HashMap<Character, Integer> counter = new HashMap<>(26);
for (int i = 0; i < s.length(); i++) {
counter.put(s.charAt(i), counter.getOrDefault(s.charAt(i), 0) + 1);
}
for (char c : counter.keySet()) {
if (counter.get(c) < k) {
int res = 0;
for (String t : s.split(String.valueOf(c))) {
res = Math.max(res, longestSubstring(t, k));
}
return res;
}
}
return s.length();
}
}
评论