[Leetcode]Minimum Window Substring

来源:互联网 发布:医疗器械专用软件 编辑:程序博客网 时间:2024/06/10 21:41
class Solution {
public:
    string minWindow(string s, string t) {
        vector<int> found(128,0);
        vector<int> needfound(128,0);
        for(int i=0;i<t.size();i++)
        needfound[t[i]]++;
        found[s[0]]++;
        
        int count=t.size();
        if(found[s[0]]<=needfound[s[0]])
        count--;
        int result=INT_MAX;
        int end=0;int begin=0;int len;
        int minbegin,minlen;
        while(true)
        {
            if(count==0)
            {
                while(found[s[begin]]>needfound[s[begin]])
                {
                    found[s[begin]]--;
                    begin++;
                }
                len=end-begin+1;
                if(len<result)
                {
                    minbegin=begin;
                    result=len;
                }
            }
            
            if(end<s.size())
            {
                end++;
                found[s[end]]++;
                if(found[s[end]]<=needfound[s[end]])
                count--;
            }
            else
            break;
        }
        if(result!=INT_MAX)
        return s.substr(minbegin,result);
        else
        return "";
    }
};
0 0
原创粉丝点击