Trains the technique from
LeetCode 395Longest Substring with At Least K Repeating CharactersThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
An intake log is the lowercase string s, one letter per event, listed in the order the events arrived.
Take any stretch of consecutive events. Call the stretch solid when every letter that occurs anywhere inside it occurs at least k times inside it. Return the length of the longest solid stretch, or 0 when no non-empty stretch is solid.
Example 1
The stretch of events 0 through 5 spells aaabbb, where a occurs three times and b occurs three times, so it is solid and its length is 6.
Example 2
Each of a, b and c occurs twice in the whole log, so no letter reaches three occurrences anywhere and no non-empty stretch is solid.
Example 3
The whole log holds a twice and b twice, which meets the requirement of at least two occurrences each, so the answer is its full length 4.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def longest_substring(s: str, k: int) -> int:public int longestSubstring(String s, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.