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.
思路:这道题最小覆盖子串,意思就是说在S中找到能全部覆盖T中元素的最小子串。使用两个指针begin和end,以及两个hashtable(hastable1和hastable2),hastable1表示T中元素的个数,hastable2表示当前收集到的T中元素的个数;count表示当前收集到的字母总数,当然这个字母是T中元素。当count等于T的长度时,我们就找到了一个满足条件的window。用指针end来遍历S,当遇到一字母ch,且ch是T中的字母,则hastable2[ch]加1,如果hastable2[ch]<=hastable1[ch],则count也要增加1.当count为T长度时,我们递增begin,同时保证count为T长度。
这里面有个很重要的问题,就是如何保证count始终为T的长度?假设begin指向字母ch,如果hastable2[ch]>hastable1[ch],则hastable2[ch]--,同时begin++,去除冗余的字母。这样一个合法的window出现,记录开始和结尾指针,同时更新最小长度。
class Solution { public: string minWindow(string S, string T) { int sLen=S.size(); int tLen=T.size(); if(sLen<=0||tLen<=0) return ""; int minBeg,minEnd; int minWindow=sLen+1; int count=0; int hastable1[256]={0},hastable2[256]={0}; for(int i=0;i<tLen;i++) { hastable1[T[i]]++; } for(int begin=0,end=0;end<sLen;end++) { if(hastable1[S[end]]==0) continue; char ch=S[end]; hastable2[ch]++; if(hastable2[ch]<=hastable1[ch]) count++; if(count==tLen) { while(hastable1[S[begin]]==0||hastable1[S[begin]]<hastable2[S[begin]]) { if(hastable1[S[begin]]<hastable2[S[begin]]) hastable2[S[begin]]--; begin++; } int length=end-begin+1; if(length<minWindow) { minBeg=begin; minEnd=end; minWindow=length; } } } return minWindow<=sLen?S.substr(minBeg,minWindow):""; } };
参考:最小覆盖子串