Leetcode-minimum window Substring--Problems with Windows

Source: Internet
Author: User

Title Description

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.

Find the smallest window that contains all the letters of T.

Note: S=bba T=ba. At this time right=2, left=0, move left will ++m[b], but there is still a B in the window. So in the right move, m[b]=-1, that is, as long as the inclusion is--, and only m[b]>=0, is effective, will ++count. Also, when you move left, Count--。 only when you are m[b]>0.

This problem is a string processing topic, and substring with concatenation of the all words thinking very similar, is also to build a dictionary, and then maintain a window. The difference is in this topic, because you can skip the characters that are not in the dictionary (that is, the string does not need to contain and contains only the characters in the dictionary, some are not in the dictionary can still meet the requirements), so encountered in the dictionary can continue to move the characters in the right side of the window, The condition of moving the left side of the window is that when the string that satisfies the condition is found, the window is moved until the character in the dictionary is no longer in the window. In the implementation is to maintain a hashmap, the beginning of the key contains all the characters in the dictionary, value is the number of characters, and then encounter the characters in the dictionary, the corresponding number of characters minus one. The time complexity of the algorithm is O (n), where n is the length of the string, because each character is not accessed more than two times during the Maintenance window. The spatial complexity is O (the size of the dictionary), which is the length of T in the code. The code is as follows:
1 classSolution {2  Public:3     stringMinwindow (stringSstringT) {4         if(s.length () = =0)5             return "";6map<Char,int>m;7          for(intI=0; I<t.length (); i++){8             if(M.find (t[i])! =m.end ())9m[t[i]]++;Ten             Else Onem[t[i]]=1; A         } -         intleft=0, right=0, Min=s.length () +1, minstart=0, count=0; -          for(right=0; Right<s.length (); right++){ the             if(M.find (s[right]) = =m.end ()) -                 Continue; -m[s[right]]--; -             if(m[s[right]]>=0){ +count++; -             } +              while(count==t.length ()) { A                 if((right-left+1) <min) { atmin=right-left+1; -minstart=Left ; -                 } -                 if(M.find (S[left])! =M.end ()) { -m[s[left]]++; -                     if(m[s[left]]>0) incount--; -                 } toleft++; +             } -         } the         stringres=""; *         if(Min!=s.length () +1){ $res=s.substr (minstart,min);Panax Notoginseng         } -         returnRes; the     } +};

Leetcode-minimum window Substring--Problems with 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.