Given a string s and an integer k, find the number of substrings of length k with no repeated characters.
Example 1:
Input: s = "havefunonleetcode", k = 5
Output: 6
Explanation:
There are 6 substrings of length 5 with no repeated characters:
- havef
- avefu
- vefun
- efuno
- funon
- unonl
leetcode
Example 2:
Input: s = "home", k = 5
Output: 0
Explanation:
There are 0 substrings of length 5 in the string "home".
Constraints:
1 <= s.length <= 104s consists of lowercase English letters.1 <= k <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for this problem involves examining every possible substring of a specified length within the given string. We check each of these substrings to determine if it contains any repeated characters. If a substring has the correct length and no repeated characters, we count it.
Here's how the algorithm would work step-by-step:
def find_substrings_with_no_repeated_characters_brute_force(main_string, substring_length):
number_of_substrings_found = 0
# Iterate through all possible starting positions of substrings
for index in range(len(main_string) - substring_length + 1):
substring = main_string[index:index + substring_length]
# Use a set to track characters, checking for repeats
character_set = set()
has_repeated_characters = False
for character in substring:
# If char already in set, it's a repeat; break
if character in character_set:
has_repeated_characters = True
break
character_set.add(character)
# Increment the counter if no repeats were found
if not has_repeated_characters:
number_of_substrings_found += 1
return number_of_substrings_foundWe want to find all the small pieces of the big string that have the right length and contain only unique letters. Instead of checking every possible piece, we'll use a 'sliding window' which helps us efficiently check each part once.
Here's how the algorithm would work step-by-step:
def find_k_length_substrings_with_no_repeated_characters(input_string, substring_length):
string_length = len(input_string)
if substring_length > string_length:
return 0
substring_count = 0
for i in range(string_length - substring_length + 1):
substring = input_string[i:i + substring_length]
# Use a set to efficiently detect repeated characters.
if len(set(substring)) == substring_length:
substring_count += 1
return substring_count| Case | How to Handle |
|---|---|
| Null or empty input string | Return an empty list immediately as there are no substrings to process. |
| K is zero or negative | Return an empty list as a substring of non-positive length is not meaningful. |
| K is greater than the length of the input string | Return an empty list, because no substring of length K can exist in the string. |
| Input string contains non-ASCII characters (Unicode) | The character frequency map must handle the full range of Unicode characters correctly, potentially requiring a larger data structure. |
| All characters in the input string are the same | Return an empty list because if all characters are identical no substring of length greater than 1 can be composed of distinct chars. |
| Input string is very long and K is relatively small | The sliding window approach should scale linearly with the length of the input string. |
| Input string contains many overlapping valid substrings | The algorithm must correctly identify and return all valid substrings, without missing any due to the sliding window process. |
| K is 1 | Return a list of each individual character of the input string, as each single character substring is inherently unique. |