llllmz

导航

76. 最小覆盖子串

class Solution {
public:
    map<char, int> maps, mapt;
    bool isContained(){
        for(pair<char, int> elem : mapt){
            if(elem.second > maps[elem.first]) return false;
        }
        return true;
    }
    string minWindow(string s, string t) {
        int left = 0, right = 0;
        int minLength = INT_MAX, ansL = -1;
        for(int i = 0; i < t.size(); ++i) ++mapt[t[i]];
        while(left <= right && right < s.size()){
            ++maps[s[right]];
            if(mapt.find(s[right]) != mapt.end()){ 
                while(isContained()){
                    if(minLength > right - left + 1){
                        minLength = right - left + 1;
                        ansL = left;
                    }
                    --maps[s[left++]];
                }
            }
            ++right;
        }
        if(ansL == -1) return string("");
        return s.substr(ansL, minLength);
    }
};

 

posted on 2024-09-15 20:36  神奇的萝卜丝  阅读(7)  评论(0)    收藏  举报