Longest Substring with At Least K Repeating Characters - Problem
Given a string s and an integer k, return the length of the longest substring of s such that the frequency of each character in this substring is greater than or equal to k.
If no such substring exists, return 0.
Note: A substring is a contiguous sequence of characters within a string.
Input & Output
Example 1 — Basic Case
$
Input:
s = "aaabb", k = 3
›
Output:
3
💡 Note:
The longest substring is "aaa" where character 'a' appears 3 times, which meets the requirement k=3.
Example 2 — No Valid Substring
$
Input:
s = "ababbc", k = 2
›
Output:
5
💡 Note:
The longest substring is "ababb" where 'a' appears 2 times and 'b' appears 3 times, both ≥ k=2.
Example 3 — Impossible Case
$
Input:
s = "abc", k = 4
›
Output:
0
💡 Note:
No character appears 4 times since the string length is only 3, so return 0.
Constraints
- 1 ≤ s.length ≤ 104
- s consists of only lowercase English letters
- 1 ≤ k ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
String with character frequency requirement
2
Find Valid Substrings
Identify substrings where all chars appear ≥ k times
3
Return Length
Length of the longest valid substring
Key Takeaway
🎯 Key Insight: Characters that appear less than k times globally can never be part of a valid substring
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code