Substring with concatenation of all words

August 13, 2022

sliding-window

Problem URL: Substring with concatenation of all words

We will create a haspmap with all the words count from the list, then we will start a sliding window, and checking the word is present in the string, then move forward by the length of each word.

class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        wordsLen = len(words)
        wordLen = len(words[0])
        res = []

        for i in range(wordLen):
            left = i
            count = collections.Counter(words)
            for j in range(left, len(s)+1-wordLen, wordLen):
                word = s[j:j+wordLen]
                count[word] -= 1

                while count[word] < 0:
                    count[s[left:left+wordLen]] += 1
                    left += wordLen
                if left + wordLen*wordsLen == j + wordLen:
                    res.append(left)

        return res

Time Complexity: O(n*w), n is the number of words and w is the length of each word
Space Complexity: O(n*w)