1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class Solution { public: string minWindow(string s, string t) { string res = ""; int len = s.size(); int i = 0, j = 0, diff = t.size(); unordered_map<char, int> map; for (auto item : t) map[item] ++;
while (j < s.size()) { if (map.count(s[j])) { if (map[s[j]] > 0) diff --; map[s[j]] --; } if (diff == 0) { while (i < j && (!map.count(s[i]) || map[s[i]] < 0)) { if (map.count(s[i])) map[s[i]] ++; i ++; } res = len < j - i + 1? res : s.substr(i, j - i + 1); len = res.size(); map[s[i ++]] ++; diff ++; } j ++; } return res; } };
|