Leetcode#30 Substring with concatenation of all Words

Source: Internet
Author: User
Tags traits

Original title Address

Look at the words in L as a whole, which is similar to Minimun window string, which uses a sliding window to search.

So, in turn, enumerate all the start position I of the s, starting from I to search. Of course, it is not necessary to enumerate all i,i equal to the word length in L-1. For example, l in the word length of 3, then when the enumeration over i=0,1,2, no longer try to i=3, because the results have been covered by the i=0 situation.

Use the left and right two pointers (Left,right) to point to the left-to border of the window, moving the pointer as follows:

1. Expand the window. You might want to set the word "w" to the right and observe W:

If W is not in L, reset the window so that the window crosses W, i.e. right++, left = right

Otherwise, it may be possible to set the number of times that the W appears in the window is window[w],w in L TRAITS[W].

If WINDOW[W] < traits[w], stating that this word is not enough, then expand the window, move right to a unit, do not forget window[w]++

If window[w] = = Traits[w], indicating that the current window may constitute a set of solutions, then stop the expansion window and skip to step 3rd (check).

If WINDOW[W] > Traits[w], indicating that W appears too many times, can not be placed in the window, then stop the expansion window, jump to the 2nd step (shrinking window), the purpose is to remove the extra W window.

2. Shrink the window. The 1th step is to ensure that the words encountered at this time must appear in L.

Come here to explain that there must be a lot of words, you may assume that is W, then constantly right to move the left contraction window, each window[*left]--, until window[w] = = Traits[w], end contraction

3. Check if it is a set of solutions

This step is simple, see if the window length is equal to the length of L and can, because the first two steps to ensure that any word w must appear in L, and Window[w] <= traits[w]. If it is a solution, join the result set.

Then return to the 1th step and continue to the next cycle until the window moves to the s right edge.

Code:

1vector<int> findsubstring (stringS, vector<string> &L) {2vector<int>Res;3map<string,int>traits;4 5   if(L.empty () | | S.empty ())returnRes;6 7    for(auto s:l)8traits[s]++;9   intLen = l[0].length ();Ten  One    for(inti =0; i < Len; i++) { Amap<string,int>window; -  -     intL =i; the     intR =i; -      while(R <s.length ()) { -        while(R <s.length ()) { -         stringWord =S.substr (R, Len); +R + =Len; -         if(Traits.find (word) = =Traits.end ()) { + window.clear (); AL =R; at         } -         Else { -window[word]++; -           if(Window[word] >=Traits[word]) -              Break; -         } in       } -        while(L <r) { to         stringHead =s.substr (L, Len); +         stringTail = S.SUBSTR (R-Len, Len); -         if(Window[tail] = =Traits[tail]) the            Break; *L + =Len; $window[head]--;Panax Notoginseng       } -       if(R-l = = Len *l.size ()) { the Res.push_back (l); +       } A     } the   } +  -   returnRes; $}

Leetcode#30 Substring with concatenation of all Words

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.