leetcode-----76. 最小覆盖子串

代码

class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char, int> hs, ht;
        for (auto c: t) ht[c]++;

        string ans;
        int cnt = 0;
        for (int i = 0, j = 0; i < s.size(); ++i) {
            hs[s[i]]++;
            if (hs[s[i]] <= ht[s[i]]) cnt++;
            while (hs[s[j]] > ht[s[j]]) hs[s[j++]]--;
            if (cnt == t.size()) {
                if (ans.empty() || i - j + 1 < ans.size()) {
                    ans = s.substr(j, i - j + 1);
                }
            }
        }
        return ans;
    }
};
原文地址:https://www.cnblogs.com/clown9804/p/13300711.html