Find K-Length Substrings With No Repeated Characters - Problem
Given a string s and an integer k, return the number of substrings in s of length k with no repeated characters.
A substring is a contiguous sequence of characters within a string.
Example:
- If
s = "havefunonleetcode"andk = 5, the substring"havef"has repeated character 'e', but"onlee"has repeated 'e', while"funon"has no repeated characters.
Input & Output
Example 1 — Basic Case
$
Input:
s = "havefunonleetcode", k = 5
›
Output:
5
💡 Note:
The valid substrings of length 5 are: "havef" (h,a,v,e,f - all unique), "vefun" (v,e,f,u,n - all unique), "efuno" (e,f,u,n,o - all unique), "etcod" (e,t,c,o,d - all unique), and "tcode" (t,c,o,d,e - all unique). Total count is 5.
Example 2 — Shorter String
$
Input:
s = "home", k = 3
›
Output:
2
💡 Note:
The substrings are: "hom" (all unique), "ome" (all unique). Both have no repeated characters, so answer is 2.
Example 3 — All Same Characters
$
Input:
s = "aaa", k = 2
›
Output:
0
💡 Note:
All possible substrings of length 2 are "aa" which has repeated character 'a', so no valid substrings exist.
Constraints
- 1 ≤ s.length ≤ 104
- 1 ≤ k ≤ 26
- s consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code