Ctrl K

Windows Without Repeats

Medium
You are given a string text of lowercase English letters and a number width. Count how many of its substrings of length width (contiguous windows) consist of pairwise distinct letters. Windows starting at different positions count separately, even if their contents match. If width exceeds the length of the string, the answer is 0.
Example 1
text="banana", width=2 → 5

The windows are ba, an, na, an, na; all five have distinct letters. Identical windows at different places count separately.

Example 2
text="letter", width=3 → 2

The windows are let, ett, tte, ter; only let and ter qualify.

Example 3
text="abc", width=5 → 0

A window longer than the string does not fit.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • 1 ≤ width ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times Medium
loading Python…
Input
text="banana", width=2
Expected
5

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