Ctrl K

A Letter Reaches k Repeats: Large Input

Hard
You are given a string text of lowercase English letters and an integer times. Count the contiguous substrings of text in which **at least one** letter occurs times times or more. Substrings with the same content but at different positions count separately. The string can be hundreds of thousands of characters long, so checking every substring will be too slow.
Example 1
text="xyxzy", times=2 → 4

The qualifying substrings are "xyx", "xyxz", "xyxzy" (two xs each) and "yxzy" (two ys), 4 in total.

Example 2
text="abc", times=1 → 6

With times = 1, all 6 substrings qualify.

Example 3
text="bbbb", times=3 → 3
Constraints
  • 1 ≤ len(text) ≤ 3 · 10⁵
  • 1 ≤ times ≤ len(text)
  • text consists of lowercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWord Blocks in Any Order HardShortest Covering Window HardLongest Clean Stretch HardEven Stretches Without Jumps HardWindows Covering a Letter Set: Large Input Hard
loading Python…
Input
text="xyxzy", times=2
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.