Simple c++ solution


  • 0
    I
    string minWindow(string s, string t) {
        vector<int> t_count(256, 0), s_count(256,0);    
        int total = t.length();
        int g_s =0, g_c = INT_MAX, start=0;
        for (auto i: t) t_count[i]++;
        for (int i =0; i<s.length(); ++i)
        {
            ++s_count[s[i]];
            if (s_count[s[i]]<=t_count[s[i]]) --total;
            if (total==0)
            {
                while(s_count[s[start]]>t_count[s[start]])  
                {
                    --s_count[s[start]];
                    ++start;
                }
                
                if(i-start+1<g_c)
                {
                    g_c = i-start+1;
                    g_s = start;
                }
            }
        }
        if (g_c==INT_MAX) return string();
        else return s.substr(g_s, g_c);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.