Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters.
Example 1:
Input: s = "eceba", k = 2
Output: 3
Explanation: The substring is "ece" which its length is 3.
Example 2:
Input: s = "aa", k = 1
Output: 2
Explanation: The substring is "aa" which its length is 2.
Constraints:
1 <= s.length <= 5 * 1040 <= k <= 50When 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 approach to finding the longest stretch of text with a limited variety of symbols is to examine every single possible starting point and ending point in the text. For each and every stretch you look at, you count how many different types of symbols appear inside it to see if it stays within your limit, and you keep track of the longest one that works.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring_k_distinct(input_string, character_limit):
maximum_length_found = 0
# Iterate through every possible starting position to evaluate all potential substrings
for start_position in range(len(input_string)):
for end_position in range(start_position, len(input_string)):
current_substring = input_string[start_position:end_position + 1]
unique_characters_set = set()
for character in current_substring:
unique_characters_set.add(character)
# Validate if the unique symbol count stays within the allowed limit before measuring
if len(unique_characters_set) <= character_limit:
current_length = (end_position - start_position) + 1
# Keep track of the maximum length found so far to ensure we return the optimal result
if current_length > maximum_length_found:
maximum_length_found = current_length
return maximum_length_foundInstead of testing every single possible section of the text from scratch, we use a smart sliding window approach that expands and shrinks dynamically. This allows us to keep track of our character variety efficiently without wasting effort rechecking parts we already looked at.
Here's how the algorithm would work step-by-step:
def length_of_longest_substring_k_distinct(input_string, allowed_distinct_limit):
character_frequency_map = {}
window_start_index = 0
maximum_substring_length = 0
if allowed_distinct_limit == 0:
return 0
for window_end_index in range(len(input_string)):
current_character = input_string[window_end_index]
character_frequency_map[current_character] = character_frequency_map.get(current_character, 0) + 1
# Shrink window from the left if distinct character count exceeds the allowed limit.
while len(character_frequency_map) > allowed_distinct_limit:
left_side_character = input_string[window_start_index]
character_frequency_map[left_side_character] -= 1
if character_frequency_map[left_side_character] == 0:
del character_frequency_map[left_side_character]
window_start_index += 1
# Track the maximum valid window width found during the expansion and contraction.
current_window_width = window_end_index - window_start_index + 1
if current_window_width > maximum_substring_length:
maximum_substring_length = current_window_width
return maximum_substring_length| Case | How to Handle |
|---|---|
| String length is zero or k is zero | Return zero immediately since no valid substring can be formed. |
| String length is less than k | Return the total length of the string because all characters are distinct and within the limit. |
| k is greater than the number of unique characters in the entire string | Return the length of the string as the entire string satisfies the condition. |
| String contains all identical characters | The sliding window expands to the full string length because the distinct character count remains one. |
| String consists entirely of distinct characters and k equals one | The window size collapses to one, and the algorithm correctly returns a maximum length of one. |
| Extremely large string inputs | The O(n) sliding window approach ensures linear time complexity and handles large inputs efficiently. |
| String contains special characters, spaces, or unicode points | Using a hash map or frequency array sized for the character set correctly tracks all distinct entities. |