395. Longest Substring with At Least K Repeating Characters

代码1

Runtime: 0 ms, faster than 100.00% of Java online submissions for Longest Substring with At Least K Repeating Characters.

class Solution {
    public int longestSubstring(String s, int k) {
        return helper(s.toCharArray(), 0, s.length() - 1, k);
    }
    public int helper(char[] c, int start, int end, int k) {
        if (end - start + 1 < k) return 0;
        int[] count = new int[26];
        for (int i = start; i <= end; i++) {
            count[c[i] - 'a']++;
        }
        for (int i = start; i <= end; i++) {
            if (count[c[i] - 'a'] < k) {
                int j = i + 1;
                while (j <= end && count[c[j] - 'a'] < k) j++;
                return Math.max(helper(c, start, i - 1, k), helper(c, j, end, k));
            }
        }
        return end - start + 1;
    } 
}

你可能感兴趣的:(395. Longest Substring with At Least K Repeating Characters)