[Leetcode] Minimum window substring min character windows

Source: Internet
Author: User

Given a string S and a string T, find the minimum window in S which would 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 so covers all characters in T, return the emtpy string "".

If There is multiple such windows, you is guaranteed that there would always being only one unique minimum window in S.

Test instructions: Finds substrings in S that contain a minimum of T, regardless of alphabetical order.

Idea: Sort by bucket. First put all the elements in t into the hash table, and then traverse the string s. If the current character is not in the hash table, it means that this is not in T and is skipped directly. If so, then the number of the corresponding characters in the hash table minus 1, indicating that the characters in T has been found one, at this time, if the number of corresponding characters is not less than 0, the character is valid. What's the point? such as: string s= "ABAC" to find t= "AC", the first time, find "a", the number of a corresponding to the hash table becomes 0, that is, a valid a, counter count noted the number of A; Once again, the description T has only one a, and has been noted, so do not need to count again. That is, ignore duplicates and find all the lines in T. That is clearly the second a more in line with test instructions, why to write down the first A, this will be in the back slowly explain.

This is done until each character in the T is noted and recorded only once. At this point, we update the minimum length of the test instructions substring. At this point, we have a problem, that is, if the beginning, there are many characters are not in T, but we note that the smallest string includes these, what to do? We just keep moving the left pointer to the right until we encounter the first character in T, and in this process we constantly update the Minlen length so that we get the length of the substring of this compound test instructions.

So how do we get the next length that fits test instructions? At this point, we only need to add the leftmost character in the hash table in the number of 1, and then count minus 1, the implementation, re-look for the next substring of the loop. Because, will go through the s, so said before encountering the second A, will re-update Minlen, so just remember a. Reference to Grandyang's blog. The code is as follows:

1 classSolution {2  Public:3     stringMinwindow (stringSstringT)4     {5         if(T.size () >s.size ())return "";6         stringres="";7         intleft=0, count=0, Minlen=s.size () +1;8unordered_map<Char,int>m;9          for(intI=0; I<t.size (); + +i)Ten         { One             if(M.find (t[i])! =m.end ()) A++M[t[i]]; -             Else -m[t[i]]=1; the         }     -  -          for(intright=0; Right<s.size (); + +Right ) -         { +             if(M.find (s[right])! =m.end ()) -             { +--M[s[right]]; A                 if(m[s[right]]>=0) at++count; -                  while(count==t.size ()) -                 { -                     if(right-left+1<Minlen) -                     { -minlen=right-left+1; inres=s.substr (Left,minlen); -                     } to                     if(M.find (S[left])! =m.end ()) +                     { -++M[s[left]]; the                         if(m[s[left]]>0) *--count; $                     }Panax Notoginsengleft++; -                 } the             } +         } A         returnRes; the     } +};

problem-solving ideas : In fact, through a double pointer to maintain a window, the right mouse pointer expansion to find the purpose of containing substrings, the window left pointer to the right contraction to minimize the substring. The implementation of a typical sliding window method.

You can also use an array to replace the hash table, refer to highbrow-Jian's blog, the code is as follows:

1 classSolution {2  Public:3     stringMinwindow (stringSstringT)4     {5         intSlen=s.size (), tlen=t.size ();6         if(slen==0|| tlen==0)    7             return "";8vector<int> Shash ( the,0), Thash ( the,0);9          for(intI=0, i<tlen;++i)Ten         { Onethash[t[i]]++; A         }     -  -         intbeg=-1, end=slen,count=0, minlen=Slen; the          for(intI=0, start=i;i<slen;++i) -         { -++Shash[s[i]]; -             if(shash[s[i]]<=Thash[s[i]]) +++count; -  +             if(count==TLen) A             { at                  while(start<i&&shash[s[start]]>Thash[s[start]]) -                 { ---Shash[s[start]]; -start++; -                 } -  in                 if(i-start<Minlen) -                 { tominlen=i-start; +beg=start; -End=i; the                 } *  $--shash[s[start++]]; Find the next qualifying substringPanax Notoginseng--count; -             } the         } +         if(beg==-1) A             return ""; the         returnS.substr (beg,end-beg+1); +  -     } $};

This method looks a little more clearly.

[Leetcode] Minimum window substring min character windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.