Given two strings s
and t
of lengths m
and n
respectively, return the minimum window substring of s
such that every character in t
(including duplicates) is included in the window. If there is no such substring, return the empty string ""
.
The testcases will be generated such that the answer is unique.
Example 1:
Input: s = "ADOBECODEBANC", t = "ABC" Output: "BANC" Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Example 2:
Input: s = "a", t = "a" Output: "a" Explanation: The entire string s is the minimum window.
Example 3:
Input: s = "a", t = "aa" Output: "" Explanation: Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.
Constraints:
m == s.length
n == t.length
1 <= m, n <= 105
s
and t
consist of uppercase and lowercase English letters.Follow up: Could you find an algorithm that runs in O(m + n)
time?
When 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 for finding the smallest section of text containing specific characters involves checking every possible section, one by one. We'll look at progressively larger sections until we find one that has all the characters we need. We then remember the smallest section we found that meets the requirement and continue until no more sections are possible.
Here's how the algorithm would work step-by-step:
def min_window_substring_brute_force(text_string, characters_to_include):
minimum_window = ""
minimum_window_length = float('inf')
for starting_index in range(len(text_string)):
for ending_index in range(starting_index, len(text_string)):
substring = text_string[starting_index:ending_index + 1]
# Verify this substring contains all target chars
if all_characters_present(substring, characters_to_include):
# Keep track of the min valid window
current_window_length = len(substring)
if current_window_length < minimum_window_length:
minimum_window_length = current_window_length
minimum_window = substring
return minimum_window
def all_characters_present(substring, characters_to_include):
for character in characters_to_include:
if character not in substring:
return False
return True
We need to find the smallest part of a larger piece of text that contains all the characters from a specific set of characters. Instead of checking every possible part of the larger text, we cleverly slide a window along the text, expanding or shrinking it as needed to find the minimum sized part that meets our requirements.
Here's how the algorithm would work step-by-step:
def minimum_window_substring(text_string, characters_to_include):
if not text_string or not characters_to_include:
return ""
required_character_counts = {}
for character in characters_to_include:
required_character_counts[character] = required_character_counts.get(character, 0) + 1
window_start = 0
window_end = 0
formed = 0
window_character_counts = {}
minimum_window_length = float('inf')
minimum_window_start = 0
while window_end < len(text_string):
character = text_string[window_end]
window_character_counts[character] = window_character_counts.get(character, 0) + 1
if character in required_character_counts and \
window_character_counts[character] == required_character_counts[character]:
formed += 1
# Shrink the window while all characters are included.
while formed == len(required_character_counts):
if window_end - window_start + 1 < minimum_window_length:
minimum_window_length = window_end - window_start + 1
minimum_window_start = window_start
character = text_string[window_start]
window_character_counts[character] -= 1
# Decrement 'formed' only if the char is required and its count drops.
if character in required_character_counts and \
window_character_counts[character] < required_character_counts[character]:
formed -= 1
window_start += 1
window_end += 1
# If no valid window was found.
if minimum_window_length == float('inf'):
return ""
else:
# Return the minimum window substring.
return text_string[minimum_window_start : minimum_window_start + minimum_window_length]
Case | How to Handle |
---|---|
Empty string s or t | Return an empty string immediately since a substring cannot be formed. |
String t is longer than string s | Return an empty string immediately as t cannot be a substring of s. |
String t contains characters not present in string s | The algorithm should return an empty string since a substring fulfilling the requirement is impossible. |
String s and t are identical | Return s itself as it is the minimum window. |
String t contains duplicate characters and s doesn't have enough of them | The algorithm will correctly determine that no substring exists and return an empty string. |
String s is very long and contains multiple valid substrings of t | The sliding window approach efficiently finds the minimum window among all valid substrings, and scales linearly with s length. |
String t contains all the same characters. | The algorithm should find the smallest contiguous block of those characters in s. |
String s or t contain unicode or extended ASCII characters. | Ensure the character frequency maps support the full character set, often requiring using wider character types or libraries. |