LeetCode-Minimum Window Substring -- 窗口问题

题目描述

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).

For example,
S ="ADOBECODEBANC"
T ="ABC"

Minimum window is"BANC".

Note: 
 If there is no such window in S that covers all characters in T, return the emtpy string"".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

找出包含T所有字母的最小窗口。

注意:S=bba t=ba。此时right=2时,left=0,移动left时会++m[b],但是此时窗口内仍旧有一个b。所以在right移动时,m[b]=-1,即只要包含即--,而只有m[b]>=0时,才是有效的,才会++count。同样,在移动left时也是,只有m[b]>0时,才会count--。

这道题是字符串处理的题目,和Substring with Concatenation of All Words思路非常类似,同样是建立一个字典,然后维护一个窗口。区别是在这道题目中,因为可以跳过没在字典里面的字符(也就是这个串不需要包含且仅仅包含字典里面的字符,有一些不在字典的仍然可以满足要求),所以遇到没在字典里面的字符可以继续移动窗口右端,而移动窗口左端的条件是当找到满足条件的串之后,一直移动窗口左端直到有字典里的字符不再在窗口里。在实现中就是维护一个HashMap,一开始key包含字典中所有字符,value就是该字符的数量,然后遇到字典中字符时就将对应字符的数量减一。算法的时间复杂度是O(n),其中n是字符串的长度,因为每个字符再维护窗口的过程中不会被访问多于两次。空间复杂度则是O(字典的大小),也就是代码中T的长度。代码如下:

 1 class Solution {
 2 public:
 3     string minWindow(string S, string T) {
 4         if(S.length()==0)
 5             return "";
 6         map<char, int> m;
 7         for(int i=0;i<T.length();i++){
 8             if(m.find(T[i])!=m.end())
 9                 m[T[i]]++;
10             else
11                 m[T[i]]=1;
12         }
13         int left=0,right=0,min=S.length()+1,minStart=0,count=0;
14         for(right=0;right<S.length();right++){
15             if(m.find(S[right])==m.end())
16                 continue;
17             m[S[right]]--;
18             if(m[S[right]]>=0){
19                 count++;
20             }
21             while(count==T.length()){
22                 if((right-left+1)<min){
23                     min=right-left+1;
24                     minStart=left;
25                 }
26                 if(m.find(S[left])!=m.end()){
27                     m[S[left]]++;
28                     if(m[S[left]]>0)
29                     count--;
30                 }
31                 left++;
32             }
33         }
34         string res="";
35         if(min!=S.length()+1){
36             res=S.substr(minStart,min);
37         }
38         return res;
39     }
40 };
时间: 2024-10-07 11:31:24

LeetCode-Minimum Window Substring -- 窗口问题的相关文章

LeetCode: Minimum Window Substring [076]

[题目] 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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such

[leetcode]Minimum Window Substring @ Python

原题地址:https://oj.leetcode.com/problems/minimum-window-substring/ 题意: 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). For example,S = "ADOBECODEBANC"T = "ABC" M

LeetCode &quot;Minimum Window Substring&quot; - STAR

Main reference: http://zhaohongze.com/wordpress/2014/01/04/leetcode-minimum-window-substring/ The ART of counting. So difficult and beautiful problem. It is all about dynamic window maintanence. Essentially, it is still a fancy linear scanning proced

[Leetcode] minimum window substring 最小字符窗口

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). For example,S ="ADOBECODEBANC"T ="ABC" Minimum window is"BANC". Note: If there is no such window in

[LeetCode] Minimum Window Substring 最小窗口子串

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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

Leetcode Minimum Window Substring

题目地址:https://leetcode.com/problems/minimum-window-substring/ 题目解答: import java.util.HashMap; import java.util.Map; public class Solution { public String minWindow(String S, String T) { if(S == null || S.length() == 0 || T == null || T.equals("")

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

【leetcode刷题笔记】Minimum Window Substring

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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

Minimum Window Substring leetcode java

题目: 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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such w

Minimum Window Substring leetcode

Minimum Window Substring 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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: