You're given a string s and an integer k. Your task is to count how many substrings of s contain at least one character that appears at least k times within that substring.
A substring is a contiguous sequence of characters within a string. For example, in the string "hello", some substrings are "h", "he", "ell", "hello", etc.
Example: If s = "abcab" and k = 2, we need to find substrings where at least one character appears 2 or more times. The substring "abcab" contains 'a' twice and 'b' twice, so it qualifies. The substring "bcab" contains 'b' twice, so it also qualifies.
Can you efficiently count all such valid substrings?
Input & Output
Visualization
Time & Space Complexity
O(n) for each starting position ร O(n) positions, but with early termination optimization
Hash map stores at most 26 different characters (constant space)
Constraints
- 1 โค s.length โค 3000
- 1 โค k โค s.length
- s consists of only lowercase English letters
- Time limit: 2 seconds