Find longest substring with at least k repeats
Longest Substring with At Least K Repeats
Given a string s consisting of lowercase English letters and an integer k, return the length of the longest substring of s such that every distinct character in that substring appears at least k times within the substring.
If no such substring exists, return -1.
Notes and Assumptions
- A substring is contiguous.
- If
k <= 1, the answer islen(s). - Edge cases to handle include an empty string,
k > len(s), and strings with repeated characters.
Example
s = "aaabb", k = 3→ The longest valid substring is"aaa", so the function should return3.
AI interview prep
Core dilemma
The central engineering conflict is efficiently identifying valid substrings that meet the frequency constraint while avoiding an exhaustive, computationally expensive search. This requires balancing the need to check all potential substrings against intelligent pruning or divide-and-conquer strategies to achieve optimal time complexity.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find longest substring with at least k repeats
Longest Substring with At Least K Repeats
Given a string s consisting of lowercase English letters and an integer k, return the length of the longest substring of s such that every distinct character in that substring appears at least k times within the substring.
If no such substring exists, return -1.
Notes and Assumptions
- A substring is contiguous.
- If
k <= 1, the answer islen(s). - Edge cases to handle include an empty string,
k > len(s), and strings with repeated characters.
Example
s = "aaabb", k = 3→ The longest valid substring is"aaa", so the function should return3.