使用Count函数和滑窗解决此问题
class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
from collections import Counter
a = Counter(words)
s_l = len(s)
res = []
words_l = len(words) # words的长度
words_ll = len(words[0]) # words中其中一个元素的长度
words_all = words_l*words_ll # words*words中其中一个元素的长度=总长度
words_all_ = words_all # 为了避免后面word+1会变
i = 0
while i < s_l-words_all_+1: # i不用到最后,可以少一个words长度,不减的话超时了
b = s[i:words_all] # 相当于将s=words一样的长度元素取出
bb = [b[j: j+words_ll] for j in range(0, words_all_, words_ll)]
if Counter(bb)==a:
res.append(i)
i += 1
words_all += 1
return res