[Leetcode] Minimum Window Substring

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.

See from Leetcode discuss:

O (N) solution

The method I used is to map the characters and how many is in the substring vs. many is needed.  If all the Values is non-negative, then you can remove characters from the start of the substring until you reach a negative, and if There ' s a negative, you add to the end of the substring until it's 0 again.  you continue this until you ' ve reached The end of S, and then remove characters until you has a negative count for one of the characters.

Going through the example, s= "Adobecodebanc" and t= "ABC" .  starting out, the map has the values A=-1, B=-1, C=-1, and have a count of 3 negatives.  Adding The first letter increases a to 0, which removes a negative, leaving a count of 2.  you can count the others as well, since they would never become negative, resulting in a=0,b=0,c=0,d=1,o=1,e=1 When you add the c.  Since the negative count are 0, you start removing characters from the start, which are A, droppin G it to-1, and switching back to adding at the end.

You then add to the end until your reach an A again, which results in a=0,b=1,c=0,d=2,e=2,o=2 and A count of 0. Remove from the start until your reach a negative again, which removes d,o,b,e,c, since B ' s removal only drops it to 0, not  a negative.  At this point, the substring is "Odeba" and C =-1. Add to the end until your reach a C and you has "Odebanc", and remove from the start until you get a negative again, Leavi Ng "ANC". You ' ve reached the end of the string and has a negative, so there are no shorter string remaining with all the characters.

You can retrieve the shortest substring by taking the start and end indices of the mapped substring whenever you switch fr  Om removing to adding and storing them if they is shorter than the previous shortest. If you never a switch from removing to adding and then the result is the empty string.

If s= "BANC" and t= "ABC", then the result was adding until you reach "BANC", switching to remove, hitting a negative (and th Erefore copying those lengths at 0 and 3), and attempting to add beyond the end which ends the algorithm with the Substrin G starting at 0 and ending at 3.

As every character gets adding once and removed once or less, it takes 2n steps at most to complete the algorithm, an O (n) Solution.

classSolution {Private:      intcount1[ the]; intcount2[ the];  Public:      stringMinwindow (stringSstringT) {//Start Typing your/C + + solution below//Do not write int main () function          if(t.size () = =0|| S.size () = =0)            return ""; memset (Count1,0,sizeof(count1)); memset (Count2,0,sizeof(Count2));  for(inti =0; I < t.size (); i++) {Count1[t[i]]++; Count2[t[i]]++; }                 intCount =t.size (); intStart =0; intMinSize =Int_max; intMinstart;  for(intEnd =0; End < S.size (); end++)         {             if(Count2[s[end]) >0) {Count1[s[end]]--; if(Count1[s[end]] >=0) Count--; }                          if(Count = =0)             {                  while(true)                 {                     if(Count2[s[start]) >0)                     {                         if(Count1[s[start]) <0) Count1[s[start]]++; Else                              Break; } Start++; }                              if(MinSize > End-start +1) {minSize= End-start +1; Minstart=start; }            }         }                 if(MinSize = =Int_max)return ""; stringret (S, Minstart, minSize); returnret; } };

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.