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.

 

就是说要在 S(source) 中找到包含 T(target) 中全部字母的一个子串,且这个子串一定是所有可能子串中最短的。

如果我们使用暴力解法,代码大概是这样的:

 
for (int i = 0; i < s.size(); i++)
for (int j = i + 1; j < s.size(); j++)
if s[i:j] 包含 t 的所有字母:
更新答案

思路很直接,但是显然,这个算法的复杂度肯定大于 O(N^2) 了,不好。

滑动窗口算法的思路是这样

1、我们在字符串 S 中使用双指针中的左右指针技巧,初始化 left = right = 0,把索引左闭右开区间 [left, right) 称为一个「窗口」。

2、我们先不断地增加 right 指针扩大窗口 [left, right),直到窗口中的字符串符合要求(包含了 T 中的所有字符)。

3、此时,我们停止增加 right,转而不断增加 left 指针缩小窗口 [left, right),直到窗口中的字符串不再符合要求(不包含 T 中的所有字符了)。同时,每次增加 left,我们都要更新一轮结果。

4、重复第 2 和第 3 步,直到 right 到达字符串 S 的尽头。

这个思路其实也不难,第 2 步相当于在寻找一个「可行解」,然后第 3 步在优化这个「可行解」,最终找到最优解,也就是最短的覆盖子串。左右指针轮流前进,窗口大小增增减减,窗口不断向右滑动,这就是「滑动窗口」这个名字的来历。

下面画图理解一下,needswindow 相当于计数器,分别记录 T 中字符出现次数和「窗口」中的相应字符的出现次数。

初始状态:

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

增加 right,直到窗口 [left, right] 包含了 T 中所有字符:

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

现在开始增加 left,缩小窗口 [left, right]

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

直到窗口中的字符串不再符合要求,left 不再继续移动。

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

之后重复上述过程,先移动 right,再移动 left…… 直到 right 指针到达字符串 S 的末端,算法结束。

如果你能够理解上述过程,恭喜,你已经完全掌握了滑动窗口算法思想。现在我们来看看这个滑动窗口代码框架怎么用

首先,初始化 windowneed 两个哈希表,记录窗口中的字符和需要凑齐的字符:

 
unordered_map<char, int> need, window;
for (char c : t) need[c]++;

然后,使用 leftright 变量初始化窗口的两端,不要忘了,区间 [left, right) 是左闭右开的,所以初始情况下窗口没有包含任何元素:

 
int left = 0, right = 0;
int valid = 0;
while (right < s.size()) {
// 开始滑动
}

其中 valid 变量表示窗口中满足 need 条件的字符个数,如果 validneed.size 的大小相同,则说明窗口已满足条件,已经完全覆盖了串 T

现在开始套模板,只需要思考以下四个问题

1、当移动 right 扩大窗口,即加入字符时,应该更新哪些数据?

2、什么条件下,窗口应该暂停扩大,开始移动 left 缩小窗口?

3、当移动 left 缩小窗口,即移出字符时,应该更新哪些数据?

4、我们要的结果应该在扩大窗口时还是缩小窗口时进行更新?

如果一个字符进入窗口,应该增加 window 计数器;如果一个字符将移出窗口的时候,应该减少 window 计数器;当 valid 满足 need 时应该收缩窗口;应该在收缩窗口的时候更新最终结果。

 

 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 };

 

 1 class Solution {
 2     public String minWindow(String s, String t) {
 3         
 4         if(s.length()<t.length())
 5             return "";
 6         Map<Character,Integer> wordDict = constructWordDict(t);
 7   
 8         int slow =0,minLen=Integer.MAX_VALUE,fast=0,matchCount=0,start = 0;
 9         for(fast=0;fast<s.length();fast++){
10             char ch = s.charAt(fast);
11             Integer cnt = wordDict.get(ch);
12             
13             //当前字符不在map中,fast++
14             if(cnt ==null)
15                 continue;
16             wordDict.put(ch,cnt-1);
17             
18             //当前字符在map中,且 cnt==1,需要这个match,  match总数++
19             if(cnt==1)
20                 matchCount++;
21             
22             // 如果 match的个数够了,尝试移动slow,使其更短
23             while(matchCount==wordDict.size()){
24                 //更新最短长度
25                 if(fast-slow+1<minLen){
26                     minLen = fast-slow+1;
27                     start=slow;
28                 }
29                 //移动slow
30                 char left = s.charAt(slow++);
31                 Integer leftCnt = wordDict.get(left);
32                 //当 slow 对应的字符串不在map中,说明当前字符串不需要match,继续移动
33                 if(leftCnt==null)
34                     continue;
35                 
36                 //当slow 对应的字符串在map中时,map中的key+1,
37                 wordDict.put(left,leftCnt+1);
38                 //如果slow对应的元素cnt==0,说明移动过头了,需要重新match slow对应的元素
39                 if(leftCnt==0)
40                     matchCount --;
41                 
42             }
43         }
44         return minLen==Integer.MAX_VALUE?"":s.substring(start,start+minLen);
45     }
46     private Map<Character,Integer> constructWordDict(String s){
47         Map<Character,Integer> map = new HashMap<>();
48         for(char ch :s.toCharArray()){
49             Integer cnt = map.get(ch);
50             if(cnt==null)
51                 map.put(ch,1);
52             else
53                 map.put(ch,cnt+1);
54         }
55         return map;
56     }
57 }

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

相关文章: