# Substring with concatenation of all words

Source: Internet
Author: User

You are given a string,S, And a list of words,L, That are all of the same length. Find all starting indices of substring (s) in s that is a concatenation of each word in l exactly once and without any intervening characters.

For example, given:
S:`"Barfoothefoobarman"`
L:`["Foo", "bar"]`

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

Idea: This question means that all words containing L are satisfied and there are no other interference items in the middle, and the start and end indexes of such conditions are returned. This question mainly uses the map data structure to solve the problem. It mainly uses map to record the number of occurrences of words in L. Traverse the string cyclically to find out the strings that exist in the same string as the strings in the same string and all the words in the same string must appear consecutively and completely. After finding a word, add it to the new map container. This container stores the words that meet the word list from the current pointer position, and calculates the number of times, next, if the number of times this appears in the new container is less than the number of times the container is initialized. If the value is greater than, the pointer is removed. Then find the words in other L. (Note that the words in l must appear consecutively) Finally, we move the specified number of words in the word list behind or terminate from the pointer position I due to mismatch. if the search is successful, we will record the position of the current pointer. In this way, we can find out all situations that meet the conditions.

` Class  Solution {  Public  : Vector < Int > Findsubstring ( String S, vector < String > & L ){  Int Wordnum = L. Size ();  Int Wordlen = L [ 0  ]. Size ();  Int Slen = S. Size (); vector < Int > Result; Result. Clear ();  If (Slen <= 0 | Wordnum <= 0  )  Return  Result; Map < String , Int > Words, curs;  For ( Int I = 0 ; I <wordnum; I ++ ) {Words [L [I] ++ ;} For ( Int I = 0 ; I <= (slen-wordnum * wordlen); I ++ ) {Curs. Clear ();  Int J = 0  ;  For (; J <wordnum; j ++ ){  String WORD = S. substr (I + J * Wordlen, wordlen );  If (Words. Find (Word) =Words. End ())  Break  ; Curs [word] ++ ;  If (Words [word] < Curs [word])  Break  ;}  If (J = Wordnum) {result. push_back (I );}}  Return  Result ;}}; `

Related Keywords:

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.