DSA problem
Longest Substring with At Least K Repeating Characters
Given a string s and an integer k, return the length of the longest substring of s such that every character in that substring appears at least k times.
If no such substring exists, return 0.
The tricky part is that the "every character appears at least k times" condition is not monotonic, so a naive single-window scan does not work directly. The standard technique fixes the number of distinct characters allowed in the window (from 1 to 26), and for each fixed count runs a valid sliding window. For a fixed distinct count, the window grows and shrinks predictably, and you record windows where all present characters meet the frequency threshold.
Examples
s = "aaabb", k = 33s = "ababbc", k = 25s = "bbaaacbd", k = 33Constraints
- 1 <= s.length <= 10^4
- s consists of only lowercase English letters.
- 1 <= k <= 10^5
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.