Given a string s and an integer k, return the length of the longest substring ofssuch that the frequency of each character in this substring is greater than or equal tok.
if no such substring exists, return 0.
Example 1:
Input: s = "aaabb", k = 3
Output: 3
Explanation: The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input: s = "ababbc", k = 2
Output: 5
Explanation: The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
Constraints:
1 <= s.length <= 104
s consists of only lowercase English letters.
1 <= k <= 105
Solutions
Solution 1
Thinking
Longest substring in which every present character occurs at least \(k\) times. A distinct-count window does not capture “all \(\ge k\)”. A character that appears fewer than \(k\) times in the whole range cannot appear in any legal substring, so it is a split point.
Count the range, split on a rare character, and recurse; if none exists the range is legal. Depth is at most \(26\).