Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)

You are given a string S, and a list of words L i.e array/vector of strings (Words in list L are all of the same length). Find the starting indices of the substrings in string S, which contains all the words present in list L.

The order of words of list L appearing inside string S does not matter i.e if string S is “barfooapplefoobar” and list of words (L) is [“foo”, “bar”] then we have to look for substrings “foobar”, “barfoo” in string S.

Note : Words inside the list L can repeat.

Examples :

Input : S: "barfoothefoobarman" 
        L: ["foo", "bar"]                     
Output : 0 9
Explanation : 
// at index 0 : barfoo
// at index 9 : foobar 

Input : S: "catbatatecatatebat"
        L: ["cat", "ate", "bat"] 
Output : 0 3 9
Explanation : 
// at index 0 : catbatate
// at index 3 : batatecat
// at index 9 : catatebat    

Input : S : "abcdababcd"
        L : ["ab", "ab", "cd"]
Output : 0 2 4 
Explanation :
// at index 0 : abcdab
// at index 2 : cdabab
// at index 4 : ababcd

Input : S : "abcdababcd"
        L : ["ab", "ab"]
Output : 4

Approach :
We can use Hashing Technique to solve the above problem. Let’s see the steps :

  1. Declare a map (hash_map) which stores all words of List L corresponding to their occurrences inside list L.
  2. Traverse through all possible substrings in string S which are equal to size_L(total number of characters produced if all the words in list L are concatenated).
  3. Create a temporary map (temp_hash_map) and initialize it with original map(hash_map) for every possible substring.
  4. Extract the words from the substring and if the word is present in temp_hash_map we decrease it’s corresponding count, if it’s not present in temp_hash_map we simply break.
  5. After traversing the substring we traverse temp_hash_map and look for any key which has it’s count > 0. If we found no such key it means that all the words in list L were found in substring and store the given starting index of the substring, if we find a key which has it’s count > 0 it means we did not traversed whole substring because we came across a word which was not in temp_hash_map.

Below is the implementation of above approach :





// CPP program to calculate the starting indices
// of substrings inside S which contains all the
// words present in List L.
#include <bits/stdc++.h>
using namespace std;
// Returns an integer vector consisting of starting
// indices of substrings present inside the string S
vector<int> findSubstringIndices(string S, 
                            const vector<string>& L)
    // Number of a characters of a word in list L.
    int size_word = L[0].size();
    // Number of words present inside list L.
    int word_count = L.size();
    // Total characters present in list L.
    int size_L = size_word * word_count;
    // Resultant vector which stores indices.
    vector<int> res;
    // If the total number of characters in list L
    // is more than length of string S itself.
    if (size_L > S.size())
        return res;
    // Map stores the words present in list L
    // against it's occurrences inside list L
    unordered_map<string, int> hash_map;
    for (int i = 0; i < word_count; i++) 
    for (int i = 0; i <= S.size() - size_L; i++) {
        unordered_map<string, int> temp_hash_map(hash_map);
        int j = i,count=word_count;
        // Traverse the substring
        while (j < i + size_L) {
            // Extract the word
            string word = S.substr(j, size_word);
            // If word not found or if frequency of current word is more than required simply break.
            if (hash_map.find(word) == hash_map.end()||temp_hash_map[word]==0)
            // Else decrement the count of word from hash_map
               { temp_hash_map[word]--;count--;} 
            j += size_word;
        // Store the starting index of that substring when all the words in the list are in substring
        if (count == 0)
    return res;
// Driver Code
int main()
    string S = "barfoothefoobarman";
    vector<string> L = { "foo", "bar" };
    vector<int> indices = findSubstringIndices(S, L);
    for (int i = 0; i < indices.size(); i++)
        cout << indices[i] << " ";
    return 0;


Output :

0 9

Time Complexity : O(N – K) * K
N : length of string S.
K : total length of list L if all the words are concatenated. If L : [“ab”, “cd”] then K = 4.

My Personal Notes arrow_drop_up

Interested in problems and solving them

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : suthar_arun

Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.