Last Updated: 1 Sep, 2025

K-Repeating Substring

Moderate
Asked in company
HSBC

Problem statement

You are given a string str consisting of lowercase English letters and an integer K.

Your task is to find the length of the longest substring of str in which every character appears at least K times.


Input Format:
The first line of input contains the string str.

The second line of input contains the integer K.


Output Format:
Print a single integer representing the length of the longest valid substring.


Note:
A substring is a contiguous sequence of characters within a string.

The optimal solution uses a divide-and-conquer approach. If a character in the current substring appears less than K times, it cannot be part of the final longest substring. Therefore, the string can be split at this "invalid" character, and the problem can be solved recursively for the resulting substrings.