window = collections.defaultdict(int) needs = collections.defaultdict(int) for char in t: needs[char] += 1
match = 0 while(right < len(s)): c1 = s[right] if c1 in needs: window[c1] += 1 if window[c1] == needs[c1]: match += 1 right += 1
while match==len(needs): if (right-left < minLen): start = left minLen = right -left c2 = s[left] if c2 in needs: window[c2] -= 1 if window[c2] < needs[c2]: match -= 1 left += 1
classSolution { public: stringminWindow(string s, string t){ unordered_map<char, int> need, window; for (char c : t) { need[c]++; } int left = 0, right = 0; int count = 0; int start = 0, minLen = s.size() + 1;
while (right < s.size()) { char c = s[right]; right++; if (need.count(c)) { window[c]++; if (window[c] == need[c]) { count++; } }
while (count == need.size()) { if (right - left < minLen) { start = left; minLen = right - left; } char d = s[left]; left++; if (need.count(d)) { if (window[d] == need[d]) { count--; } window[d]--; } } } return minLen == s.size() + 1 ? "" : s.substr(start, minLen); } };