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

Example 1:

  s = "barfoothefoobarman",
  words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.


Use a sliding window. At each position, find the occurrence for each word in words and compare it to that in words. If matches, then it is a valid solution.

from collections import Counter
class Solution:
    def findSubstring(self, s, words):
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        cnt = len(words)
        if cnt == 0:
            return []
        size = len(words[0])

        if size == 0 or len(s) < cnt * size:
            return []

        hist = dict(Counter(words))
        res = []

        for idx in range(0, len(s) - cnt * size + 1):
            curr_hist = dict()
            for j in range(0, cnt):
                w = s[idx + j*size: idx + (j+1)*size]
                if w in hist:
                    if w in curr_hist:
                        curr_hist[w] += 1
                        curr_hist[w] = 1
            if curr_hist == hist:

        return res