Article From:https://www.cnblogs.com/JTechRoad/p/9060316.html

https://leetcode.com/problems/minimum-window-substring/description/

class Solution {
public:
    string minWindow(string s, string t) {
        vector<int> v(256, 0);
        for (auto c : t)
            v[c]++;
        int cnt = t.length();
        int w = 0;
        int minw_start = 0, minw_size = INT_MAX;
        for (int i = 0; i < s.length(); i++) {
            if (v[s[i]]-- > 0)  cnt--;
            while (cnt == 0) {
                if (i - w + 1 < minw_size) {
                    minw_size = i - w + 1;
                    minw_start = w;
                }
                if (v[s[w]]++ >= 0)     cnt++;
                w++;
            }
        }
        return minw_size == INT_MAX ? "" : s.substr(minw_start, minw_size);
    }
};

 

Similar Posts:

Link of this Article: 76. Minimum Window Substring

Leave a Reply

Your email address will not be published. Required fields are marked *