76. Minimum Window Substring(hard 滑动窗口)

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.

  

 1 class Solution {
 2 public:
 3     string minWindow(string s, string t) {
 4         unordered_map<char,int> need_map;
 5         unordered_map<char,int> window_map;
 6         for(auto c:t) {
 7             need_map[c]++;
 8         }
 9         int left = 0;
10         int right = 0;
11         int valid_cnt = 0;// 符合条件的char计数
12         int res_start = 0;
13         int res_len = INT_MAX;
14         while(right < s.size()) {
15 
16             char cur_c = s[right];
17             // 更新窗口数据
18             if(need_map.find(cur_c) != need_map.end()) {
19                 window_map[cur_c]++;
20                 if(window_map[cur_c] == need_map[cur_c]) 
21                     valid_cnt++;
22             }
23             // 窗口扩大
24             right++;
25 
26             printf("window: [%d, %d)\n", left, right);
27 
28             // 缩减窗口
29             while(valid_cnt == need_map.size()) { //
30                 
31                 // 更新结果
32                 if(res_len > right - left) {
33                     res_len = right - left;
34                     res_start = left;
35                 }
36                 // 缩减窗口
37                 char cur_c = s[left];
38                 left++;
39                 
40                 // 更新窗口数据
41                 if (need_map.find(cur_c)!=need_map.end()) {
42                     if(window_map[cur_c] == need_map[cur_c]) 
43                         valid_cnt--;
44                     window_map[cur_c]--;
45                 }
46             }
47         }
48         return res_len == INT_MAX ? "" : s.substr(res_start, res_len);
49     }
50 };

 

 

class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char,int> wanted_map;
        for(char c:t) {
            wanted_map[c]++;
        }

        int l =0, r = 0;
        int valid = 0;
        string res = "";
        int min_res_len = INT_MAX;
        unordered_map<char,int> cur_map;
        while(r < s.size()) {
            char c = s[r];
            if (wanted_map.find(c)!=wanted_map.end()) {
                cur_map[c]++;
                if (cur_map[c]==wanted_map[c]) {
                    valid++;
                }
            }
            r++;

            while(valid == wanted_map.size()) {
                char c2 = s[l];
                if (valid == wanted_map.size()) {
                    if (r-l < min_res_len) {
                        res = s.substr(l,r-l);
                        min_res_len = res.size();
                    }
                }
                if (wanted_map.find(c2) != wanted_map.end()) {
                    if (wanted_map[c2]== cur_map[c2]) {
                        valid--;
                    }
                    cur_map[c2]--;
                }
                l++;
            }
        }
        return res;
    }
};

 

 https://www.youtube.com/watch?v=9qFR2WQGqkU

posted @ 2018-05-07 13:34  乐乐章  阅读(247)  评论(0编辑  收藏  举报