Track needed characters and how many are fully satisfied. Contract when valid.
function minWindow(s, t): need = Counter(t) have = 0 left = 0 result = "" for right in range(len(s)): char = s[right] if char in need: need[char] -= 1 if need[char] == 0: have += 1 while have == len(need): windowSize = right - left + 1 if result == "" or windowSize < len(result): result = s[left:right+1] leftChar = s[left] if leftChar in need: if need[leftChar] == 0: have -= 1 need[leftChar] += 1 left += 1 return result
time, space.