Substring 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 that's a concatenation of each word in words exactly once and without any Intervening characters.

For example, given:
Words["foo", "bar"]

You should return the indices: [0,9] .
(Order does not matter).

Give a string s and a list of words that have the same length. Find the starting position of all the substrings, the substring contains each word in the word list, and only once, the word cannot overlap in the substring.

That is to say, substring is one of the permutations of these words, if every starting point to do a check then need to compare n*m*k times, n is the length of S, M is the length of the word, K is the number of words. However, because the length of the word is the same, you can slice s, every m characters for a paragraph, there will be a total of M-type segmentation method, the starting slice location is 0~m-1. This is equivalent to the question: In a string, a substring containing a collection of words.

The time complexity is improved to O (N*m), and the code given is as follows:

vector<int> findsubstring (stringS, vector<string>&words) {Map<string,int>dict;  for(intI=0; I<words.size (); ++i)++Dict[words[i]]; Vector<int>ret; intm =words.size (); intLen = words[0].size (); intn =s.size (); if(0==m)returnret; if(N <m)returnret;  for(intI=0; i<len; ++i) {intCNT =0; Map<string,int>curdict; intJ=i, k=i;//J is the starting position, K is the end position         while(k+len<=s.size ()) {            stringstr =S.substr (k, Len); K+=Len; if(Dict.end () = =dict.find (str)) {CNT=0;                Curdict.clear (); J=K; }            Else if(++curdict[str] >Dict[str]) {                --CURDICT[STR];  while(S.SUBSTR (j,len)! =str) {                    --curdict[s.substr (J,len)]; --CNT; J+=Len; } J+=Len; }            Else            {                ++CNT; if(CNT = =m) {ret.push_back (j); --curdict[s.substr (J,len)]; --CNT; J+=Len; }            }        }    }        returnret;}

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