While right is expanding to the end of s, there are two things need to be tracked:
class Solution:
def minWindow(self, s: str, t: str) -> str:
if len(t) > len(s):
return ''
t_count = collections.Counter(t)
s_count = collections.defaultdict(int)
satisfied = left = 0
length = float('inf')
res = [0, 0]
for right in range(len(s)):
if s[right] in t:
s_count[s[right]] += 1
if s_count[s[right]] <= t_count[s[right]]:
satisfied += 1
while satisfied == len(t):
if right - left < length:
length = right - left
res = [left, right]
if s[left] in t:
if t_count[s[left]] == s_count[s[left]]:
satisfied -= 1
s_count[s[left]] -= 1
left += 1
return '' if length == float('inf') else s[res[0]: res[1] + 1]