首页 > 其他分享 >LeetCode76. 最小覆盖子串(/滑动窗口)

LeetCode76. 最小覆盖子串(/滑动窗口)

时间:2023-02-26 20:15:28浏览次数:44  
标签:子串 cnt LeetCode76 string int 题解 ++ ori 滑动

原题解

题目

约束

题解


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;

        while (r < int(s.size())) {
            if (ori.find(s[++r]) != ori.end()) {
                ++cnt[s[r]];
            }
            while (check() && l <= r) {
                if (r - l + 1 < len) {
                    len = r - l + 1;
                    ansL = l;
                }
                if (ori.find(s[l]) != ori.end()) {
                    --cnt[s[l]];
                }
                ++l;
            }
        }

        return ansL == -1 ? string() : s.substr(ansL, len);
    }
};

标签:子串,cnt,LeetCode76,string,int,题解,++,ori,滑动
From: https://www.cnblogs.com/chuixulvcao/p/17157046.html

相关文章