A transmission is a string of lowercase English letters. Find the length of the longest contiguous substring such that every distinct letter appearing in that substring appears at least k times within it. If no non-empty substring satisfies this, the answer is 0.
Line 1: the string of lowercase English letters.
Line 2: an integer k.
A single integer: the length of the longest qualifying substring, or 0 if none exists.
Example 1
Input
aaabb 3
Expected
3
Explanation
Only the run aaa has every present letter (just a) appearing at least 3 times; adding any b breaks the rule since b appears fewer than 3 times. The answer is 3.
Example 2
Input
ababbc 2
Expected
5
Explanation
Dropping the final c (which appears once), the prefix ababb has a twice and b three times, both at least 2, giving length 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →