Find K-Length Substrings With No Repeated Characters
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
s = "abcabc", k = 3
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.
s = "home", k = 5
0
k is longer than s, so there are no substrings of length 5.
s = "aabcd", k = 2
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)
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.