Skip to content
MediumStringsAI interview only

Find K-Length Substrings With No Repeated Characters

Asked atamazongoogle

01 · Problem

Given a string s and an integer k, count how many substrings of s have length exactly k and contain no repeated characters.

Substrings are counted by position: two equal substrings that start at different indices are counted separately. If k is larger than the length of s, the answer is 0.

02 · Examples

Example 01
Input
s = "abcabc", k = 3
Output
4

The windows of length 3 are abc, bca, cab, abc. All four have distinct characters, and repeated substrings at different positions are each counted.

Example 02
Input
s = "home", k = 5
Output
0

k is longer than s, so there are no substrings of length 5.

Example 03
Input
s = "aabcd", k = 2
Output
3

The windows are aa, ab, bc, cd. Only aa has a repeated character, so the answer is 3.

03 · Constraints

  • 011 <= s.length <= 104
  • 02s consists of lowercase English letters
  • 031 <= k <= 104

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.