首页 > 其他 > 详细

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

时间:2020-07-14 19:39:37      阅读:65      评论:0      收藏:0      [点我收藏+]

代码

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;
    }
};

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

原文:https://www.cnblogs.com/clown9804/p/13300711.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!