Minimum Window Substring
Given a string s
and a string t
, find the minimum window in s
that contains all the characters of t
in any order. If there is no such window, return an empty string “”.
If there is repetitive character in t
, the returned window should contain at least the same number of that character.
The window should be of a minimum length, and you are required to find this minimum length.
Function Signature:
def min_window_substring(s: str, t: str) -> str:
"""
Find the minimum window in s that contains all characters of t.
Parameters:
- s: A string where the minimum window should be found.
- t: A string representing the characters to be included in the minimum window.
Returns:
- str: The minimum window substring or an empty string if not found.
"""
# Your code here
Example:
result = min_window_substring("ab", "a")
print(result)
assert result == "a"
# Explanation: The minimum window substring "a" contains the character "a".
result = min_window_substring("abccb", "cc")
print(result)
assert result == "cc"
# Explanation: The minimum window substring "cc" contains the characters of "cc" (two c's).
result = min_window_substring("ADOBXBCODEBANC", "ABC")
print(result)
assert result == "BANC"
# Explanation: The minimum window substring "BANC" contains all characters of "ABC".
result = min_window_substring("x", "a")
print(result)
assert result == ""
# Explanation: There is no window containing "a" in the string "x".
result = min_window_substring("a", "aa")
print(result)
assert result == ""
# Explanation: There is no window containing "aa" (two a's) in the string "a".
Note:
- The characters in the strings are case-sensitive.
- The order of characters in the minimum window does not matter.
- If there are multiple valid answers, return any valid window.
- Both
s
andt
are non-empty.
Instructions:
- Write the
min_window_substring
function to solve the problem. - Implement your solution in Python.
- Provide clear comments in your code.
- Discuss the time and space complexity of your solution.
As always, we’ll share our solution to this problem tomorrow. Stay tuned 😊