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;
    }
};
posted @ 2020-07-14 18:16  景云ⁿ  阅读(57)  评论(0编辑  收藏  举报