Longest Substring with At Least K Repeating Characters
程序员文章站
2024-03-06 20:37:44
...
https://www.lintcode.com/problem/longest-substring-with-at-least-k-repeating-characters/description
public class Solution {
private int max = 0;
/**
* @param s: a string
* @param k: an integer
* @return: return an integer
*/
public int longestSubstring(String s, int k) {
// write your code here
recursion(s, k);
return max;
}
private void recursion(String s, int k) {
int[] target = new int[26];
char[] chars = s.toCharArray();
for (char c : chars) {
target[c - 'a']++;
}
int lastIndex = 0;
for (int i = 0; i <= chars.length; i++) {
if (i == chars.length) {
if (lastIndex != 0) {
recursion(s.substring(lastIndex, i), k);
lastIndex = i + 1;
}
break;
}
char aChar = chars[i];
if (target[aChar - 'a'] < k) {
recursion(s.substring(lastIndex, i), k);
lastIndex = i + 1;
}
}
if (lastIndex == 0) {
max = Math.max(max, s.length());
}
}
}
上一篇: 【题解】CH1301 set
下一篇: CH1301 邻值查找
推荐阅读
-
Longest Substring with At Least K Repeating Characters
-
3. Longest Substring Without Repeating Characters 不含重复字母的最长子串
-
3. Longest Substring Without Repeating Characters(无重复字符的最长子串)
-
[LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
-
3. Longest Substring Without Repeating Characters/无重复字符的最长子串
-
Leetcode Two Sum (java)Longest Substring Without Repeating Characters
-
Longest Substring Without Repeating Characters
-
LeetCode - 3.Longest Substring Without Repeating Characters(388ms)
-
[LeetCode]3.Longest Substring Without Repeating Characters
-
Leetcode_3. Find the longest substring without repeating characters