LeetCode #76: Minimum Window Substring
https://leetcode.com/problems/minimum-window-substring/
To solve this problem in Python 3, we can use a sliding window approach that involves maintaining two dictionaries, count_t
and count_window
, to keep track of the count of each character in the t
string and the current window, respectively.
Here are the steps to follow:
- Initialize the
count_t
dictionary to store the count of each character in thet
string. - Initialize the
left
andright
pointers to the start of the strings
, thecount_window
dictionary to an empty dictionary, and therequired
andformed
variables to 0. - Initialize the
left_min
variable to -1, theright_min
variable to -1, and themin_len
variable to infinity. - Iterate through the string
s
using theright
pointer and add each character to thecount_window
dictionary. If the count of the current character in thecount_window
dictionary is equal to the count of the same character in thecount_t
dictionary, increment theformed
variable. - While the
formed
variable is equal to therequired
variable, i.e., all characters int
are present in the current window, update theleft_min
,right_min
, andmin_len
variables as necessary and shrink the window from the left by incrementing theleft
pointer and updating thecount_window
andformed
variables. - Return the minimum window substring.
Here’s the Python 3 code that implements this approach:
class Solution:
def minWindow(self, s: str, t: str) -> str:
count_t = {}
for char in t:
count_t[char] = count_t.get(char, 0) + 1
left = 0
right = 0
count_window = {}
required = len(count_t)
formed = 0
left_min = -1
right_min = -1
min_len = float('inf')
while right < len(s):
count_window[s[right]] = count_window.get(s[right], 0) + 1
if s[right] in count_t and count_window[s[right]] == count_t[s[right]]:
formed += 1
while left <= right and formed == required:
if right - left + 1 < min_len:
left_min = left
right_min = right
min_len = right - left + 1
count_window[s[left]] -= 1
if s[left] in count_t and count_window[s[left]] < count_t[s[left]]:
formed -= 1
left += 1
right += 1
if left_min == -1:
return ""
else:
return s[left_min:right_min+1]
Time Complexity
The time complexity of the above algorithm is O(|s| + |t|), where |s| and |t| are the lengths of the strings s
and t
, respectively.
The reason for this is that we iterate through the string s
once using the right
pointer and then shrink the window from the left using the left
pointer. In the worst case, we need to iterate through the entire string s
twice. Additionally, we iterate through the string t
once to initialize the count_t
dictionary.
Space Complexity
The space complexity of the above algorithm is O(|s| + |t|), where |s| and |t| are the lengths of the strings s
and t
, respectively.
The reason for this is that we use two dictionaries, count_t
and count_window
, to keep track of the count of each character in the t
string and the current window, respectively. The size of these dictionaries can be up to the length of the strings s
and t
.