Ubstring with concatenation of all Words

Source: Internet
Author: User

You is given a string, s, and a list of words, words, that is all of the same length. Find all starting indices of substring (s) in S, a concatenation of each word in wordsexactly once and without any I ntervening characters.

Shame!!!

English is not good, this question to see half a day to understand the meaning of the topic: In the string s to find some substrings, the sub-string of the subscript index returned to the array to save.

So what are the conditions for a substring to be fulfilled? Substrings are filled with all the words in the array words, and all words appear only once.

Note that all eligible substrings are found!!!!!

It should not be difficult to understand the problem after reading it. Note-----Array of words words should probably be duplicated.

1 classSolution {2  Public:3vector<int> findsubstring (stringS, vector<string>&words) {4vector<int>Res;5map<string,int>All ;6map<string,int>cur;7       intnums=words.size ();8        for(ints=0; s<nums;s++)9all[words[s]]++;Ten       intlen=words[0].size (); One       if(S.size () <len*nums)returnRes; A        for(intI=0; I<=s.size ()-len*nums;i++) -        { - cur.clear (); the            intJ; -             for(j=0; j<nums;j++) -            { -                stringSubs=s.substr (i+j*Len,len); +                if(All.find (Subs) ==all.end ()) Break; -cur[subs]++; +                if(Cur[subs]>all[subs]) Break; A            } at            if(j==nums) Res.push_back (i); -        } -        returnRes; -     } -};

Ubstring 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.