76. 最小覆盖子串
解题思路:滑动窗口
C++:
class Solution { public: unordered_map <char, int> ori, cnt; bool check() { for (const auto &p: ori) { if (cnt[p.first] < p.second) { return false; } } return true; } string minWindow(string s, string t) { for (const auto &c: t) { ++ori[c]; } int l = 0, r = -1; int len = INT_MAX, ansL = -1, ansR = -1; bool find_char_in_t = false; while (r < int(s.size())) { if (ori.count(s[++r]) != 0) { ++cnt[s[r]]; find_char_in_t = true; } // 不在t中的字符可以跳过 if (!find_char_in_t && ori.count(s[l]) == 0) { ++l; } while (check() && l <= r) { if (r - l + 1 < len) { len = r - l + 1; ansL = l; } if (ori.count(s[l]) != 0) { --cnt[s[l]]; } ++l; find_char_in_t = false; } } return ansL == -1 ? string() : s.substr(ansL, len); } };