美文网首页
LeetCode-76-最小覆盖子串

LeetCode-76-最小覆盖子串

作者: 突击手平头哥 | 来源:发表于2020-01-01 22:21 被阅读0次

    LeetCode-76-最小覆盖子串

    题目

    Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

    Example:

    Input: S = "ADOBECODEBANC", T = "ABC"
    Output: "BANC"
    Note:

    If there is no such window in S that covers all characters in T, return the empty string "".
    If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/minimum-window-substring
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    解题思路

    C++实现

    class Solution {
        string minLen(string a, string b)
        {
            //cout<<a<<" "<<b<<endl;
            if(a.size() > 0 && a.size() < b.size())
                return a;
            else
                return b;
        }
    public:
        string minWindow(string s, string t) {
            int slen = s.size();
            int tlen = t.size();
            if(slen < tlen || slen == 0 || tlen == 0)   return "";
    
            int l = 0, r = 0;
            map<char, int> nmap;    //记录出现次数
            int kind = 0;
            for(int i = 0; i < tlen; i++)   nmap[t[i]]++;
            kind = nmap.size();
            string res;
            map<char, int> window;
    
            int match = 0;
            while(r < slen)
            {
                if(nmap.count(s[r]))
                {
                    window[s[r]]++;
                    if(window[s[r]] == nmap[s[r]])  match++;
                }
                while(match == kind)
                {
                    while(l < r && !nmap.count(s[l])) l++;
                    res = minLen(res, s.substr(l, r-l+1));
                    window[s[l]]--;
                    if(window[s[l]] < nmap[s[l]])
                        match--;
                    l++;           
                }
                r++;
            }
            return res;
        }
    };
    

    以上代码可以通过, 不过还是可以优化的, 比如用数组代替map

    C++优化代码

    class Solution {
    public:
        string minWindow(string s, string t) {
            int slen = s.size();
            int tlen = t.size();
            if(slen < tlen || slen == 0 || tlen == 0)   return "";
    
            string res;
            int map[256] = { 0 };
            for(int i = 0; i < tlen; i++)
                map[t[i]]++;
    
            int l = 0, r = 0;
            int match = 0;
            int minLength = INT_MAX;
            while(r < slen)
            {
                map[s[r]]--;
                if(map[s[r]] >= 0)  match++;    
                //出现了一个子串中的字符
                //如果统计数小于等于0, 就表示符合要求; 后续的判断也是一样的
                while(match == tlen)
                {
                    if(r-l+1 < minLength)
                    {
                        minLength = r-l+1;
                        res = s.substr(l, r-l+1);
                    }
                    if(++map[s[l]] > 0)
                        match--;
          
                    l++;
                }
                r++;
            }
    
            return res;
        }
    };
    

    相关文章

      网友评论

          本文标题:LeetCode-76-最小覆盖子串

          本文链接:https://www.haomeiwen.com/subject/sdogoctx.html