Talentd
FleetCode

1100. Find K-Length Substrings With No Repeated Characters

Medium76.5% acceptance1 companyPremium elsewhere, free here

Given a string s and an integer k, return the number of substrings in s of length k with no repeated characters.

Example 1:

Input: s = "havefunonleetcode", k = 5
Output: 6
Explanation: There are 6 substrings they are: 'havef','avefu','vefun','efuno','etcod','tcode'.

Example 2:

Input: s = "home", k = 5
Output: 0
Explanation: Notice k can be larger than the length of s. In this case, it is not possible to find any substring.

Constraints:

  • 1 <= s.length <= 104
  • s consists of lowercase English letters.
  • 1 <= k <= 104

Asked at 1 company

Hints open one at a time

Hint 1
How to check efficiently each K-length substring?
Hint 2
First store the first leftmost K-length substring in a hashTable or array of frequencies.
Hint 3
Then iterate through the rest of characters and erase the first element and add the next element from the right. If in the hashTable we have K different character we add 1 to the counter. After that return as answer the counter.
18:22
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
"havefunonleetcode"
5
Input
"home"
5
Your code is saved on this device.Solution