Longest Window Where Every Letter Meets a Minimum Count
StringsAuto-graded · 24 test casesLast updated
Find the longest contiguous substring in which every letter that appears occurs at least k times, using a sliding window or divide and conquer.
Longest Window Where Every Letter Meets a Minimum Count
You are given a string s made of lowercase English letters and an integer k.
A substring is a contiguous block of characters of s. Call a substring k-dense if every distinct letter that appears in it occurs at least k times inside that substring.
Return the length of the longest k-dense substring of s. If there is none, return -1.
Rules and edge cases
- If
k <= 1, returnlen(s). This applies even whensis empty, in which case the result is0. - If
k >= 2andsis empty, return-1. - If
k > len(s), return-1. - Otherwise, return the length of the longest k-dense non-empty substring, or
-1if no non-empty substring qualifies. - Only the length is returned, so ties between substrings do not matter.
Constraints
scontains only characters'a'to'z'.0 <= len(s) <= 10^5kis an integer and may be zero or negative. Such values follow thek <= 1rule above.- The expected running time is roughly linear in
len(s)(up to a factor of 26).
Examples
Example 1
Input: longest_dense_window('aaabb', 3)
Output: 3
Example 2
Input: longest_dense_window('ababbc', 2)
Output: 5
Example 3
Input: longest_dense_window('abcdef', 2)
Output: -1
Example 4
Input: longest_dense_window('a', 1)
Output: 1
Hints
Hint 1
Think about what makes a substring invalid: any letter that appears fewer than k times in the whole string can never be part of a k-dense substring, so it acts as a separator.
Hint 2
Divide and conquer: count the letters, split the string at every letter whose total count is below k, and recurse on the pieces. If no letter is below k, the whole piece is valid.
Hint 3
Alternative: for each target number of distinct letters t from 1 to 26, run a sliding window that keeps at most t distinct letters and tracks how many of them have reached count k. Record the window length when both numbers equal t.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Handle the special cases first: k <= 1 returns len(s), and an empty string or k > len(s) returns -1.
For the general case, a window is valid when every distinct letter in it occurs at least k times. The number of distinct letters is hard to control directly, but there are at most 26 possible values. So for each target from 1 to 26, we look for the longest valid window that has exactly target distinct letters.
For a fixed target, run a sliding window with two pointers and maintain:
counts: the frequency of each letter in the window,distinct: how many letters have a nonzero count,enough: how many letters have a count of at leastk.
Move right forward and update the three values. While distinct > target, move left forward, undoing its effect on the counts. After that, the window has at most target distinct letters. If distinct == target and enough == target, then every letter in the window appears at least k times, so the window is valid and its length is a candidate answer.
Why it is correct
Every valid substring has some number of distinct letters t between 1 and 26. During the pass for t, the window ending at each right is the longest one with at most t distinct letters. If a valid substring with exactly t letters ends at right, the window covers it. When the window has exactly t distinct letters and all of them reach k, the window itself is valid. If the longest window ending at right is not valid, a shorter valid window ending there cannot be longer than it. Taking the maximum over all t therefore finds the longest valid substring. If no candidate is ever recorded, the answer stays -1.
Complexity
- Time:
O(26 * n), since each of the 26 passes moves both pointers at mostnsteps. - Space:
O(1), since the counts array has a fixed size of 26.
An alternative is divide and conquer. Split the string at every letter whose total count is below k and recurse on the pieces. This runs in O(26 * n) in the worst case.
Reference solution
def longest_dense_window(s, k):
n = len(s)
if k <= 1:
return n
if n == 0 or k > n:
return -1
best = -1
for target in range(1, 27):
counts = [0] * 26
left = 0
distinct = 0
enough = 0
for right, ch in enumerate(s):
i = ord(ch) - ord('a')
if counts[i] == 0:
distinct += 1
counts[i] += 1
if counts[i] == k:
enough += 1
while distinct > target:
j = ord(s[left]) - ord('a')
if counts[j] == k:
enough -= 1
counts[j] -= 1
if counts[j] == 0:
distinct -= 1
left += 1
if distinct == target and enough == target:
best = max(best, right - left + 1)
return best