Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Strings

Longest Substring with At Least K Repeats

Medium · Strings · Asked at Google, Amazon, Meta, Baidu

Print the length of the longest substring in which every letter that appears, appears at least k times. Print 0 if there is no such substring.

Input

First line: the string (lowercase letters). Second line: k.

Output

The length of the longest such substring.

Constraints

Divide and conquer: count the letters of the current range. A letter that occurs fewer than k times there can never be inside a valid substring, so split the range at every such letter and solve the pieces. If no letter is too rare, the whole range is valid. A sliding window per "number of distinct letters" (1 … 26) also works in O(26 n).

Example 1

Input

aaabb
3

Output

3

Example 2

Input

ababbc
2

Output

5

Hints

  1. A letter that occurs fewer than k times in a range can never be part of a valid substring in that range.
  2. So such a letter splits the range into independent pieces.
  3. If no letter is too rare, the whole range is valid.

Approach

solve(lo, hi): if the range is shorter than k, return 0. Count its letters. Walk through it; every position holding a letter with count < k ends a piece — solve the piece before it recursively and continue after it. If there was no such position, return hi − lo. Each recursion level removes at least one letter from consideration, so the depth is at most 26 and the work is O(26 · n).

Complexity: O(26 · n) time · O(26) depth

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

string s;
int k;

int solve(int lo, int hi) {
    if (hi - lo < k) return 0;
    int cnt[26] = {0};
    for (int i = lo; i < hi; i++) cnt[s[i] - 'a']++;
    int best = 0, start = lo;
    bool split = false;
    for (int i = lo; i < hi; i++)
        if (cnt[s[i] - 'a'] < k) {
            split = true;
            best = max(best, solve(start, i));
            start = i + 1;
        }
    if (!split) return hi - lo;
    return max(best, solve(start, hi));
}

int main() {
    cin >> s >> k;
    cout << solve(0, s.size()) << "\n";
    return 0;
}

Python solution

import sys

data = sys.stdin.read().split()
s, k = data[0], int(data[1])


def solve(lo, hi):
    if hi - lo < k:
        return 0
    cnt = [0] * 26
    for i in range(lo, hi):
        cnt[ord(s[i]) - 97] += 1
    best, start, split = 0, lo, False
    for i in range(lo, hi):
        if cnt[ord(s[i]) - 97] < k:
            split = True
            best = max(best, solve(start, i))
            start = i + 1
    if not split:
        return hi - lo
    return max(best, solve(start, hi))


print(solve(0, len(s)))

Java solution

import java.io.*;

public class Main {
    static String s;
    static int k;

    static int solve(int lo, int hi) {
        if (hi - lo < k) return 0;
        int[] cnt = new int[26];
        for (int i = lo; i < hi; i++) cnt[s.charAt(i) - 'a']++;
        int best = 0, start = lo;
        boolean split = false;
        for (int i = lo; i < hi; i++)
            if (cnt[s.charAt(i) - 'a'] < k) {
                split = true;
                best = Math.max(best, solve(start, i));
                start = i + 1;
            }
        if (!split) return hi - lo;
        return Math.max(best, solve(start, hi));
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        s = br.readLine().trim();
        k = Integer.parseInt(br.readLine().trim());
        System.out.println(solve(0, s.length()));
    }
}

Solve Longest Substring with At Least K Repeats online · All practice problems