Longest Substring with At Least K Repeating Characters
Problem Statement
You are given a string s of lowercase letters and a number k.
Find the longest substring in which every letter that appears, appears at least k times. Return its length.
If no substring qualifies, return 0.
A string s of lowercase English letters and an integer k.
The length of the longest substring where each of its letters occurs at least k times, or 0.
"aaa" works: 'a' appears 3 times. "aaabb" fails because 'b' appears only twice.
"ababb" has 'a' twice and 'b' three times. Any substring with 'c' fails, because there is only one 'c'.
- • 1 ≤ s.length ≤ 10^4
- • s contains only lowercase English letters
- • 1 ≤ k ≤ 10^5
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each start, grow right and check all 26 counts after every step
Cut the string at letters that appear < k times, solve each piece
For u = 1..26, window with at most u distinct; valid when all u have ≥ k
Try every start. Grow the end one letter at a time, keeping counts. After every step, check the 26 counts: each letter is either absent (0) or has at least k copies. If so, record the length. No early break: a substring that fails now may pass later, when more copies arrive.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.