Last Updated: 8 Dec, 2025

Longest Substring with K-Repeating Characters

Moderate
Asked in company
Amazon

Problem statement

You are given a string s consisting of lowercase English letters and an integer k. Your task is to find the length of the longest substring of s in which the frequency of every character that appears in that substring is greater than or equal to k.


If no such substring exists, return 0.


Input Format:
The first line contains the string s.

The second line contains the integer k.


Output Format:
The function should return a single integer representing the length of the longest valid substring.


Note:
This problem can be elegantly solved using a divide-and-conquer strategy. The key insight is that if a character in a given string appears fewer than k times, it can never be part of a valid substring. Therefore, such a character acts as a "splitter." We can split the string by this character and recursively find the longest valid substring in the resulting parts.