if (j+1 === wordsCount) { // Store index if we have found all the words
resultIndices.push(i);
}
Hi Shivani,
Suppose that we have 2 words in the list (our list is ["cat", "fox"]
) and our string is catfoxcat
. Now we want to find a substring catfox
and foxcat
. j
is the iterator that checks the number of words we have found in our concatenated substring. Let’s say we are checking the substring catfox
. Here, when j = 0, we have found the word cat. When j = 1, we have found the word fox. Now that we have found the substring catfox
, and we know that we have only 2 words in the list, so we do j+1
(which in this case will be 2) and compare it to the wordsCount
(which in this case is also 2). This means that we have found 2 words in the substring, and hence, we store the starting index of this substring.
Thank You!