30串联所有单词的子串

from typing import List
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
# 导入计数类
from collections import Counter
# 如果s和words其中一个为空,就返回空列表
if not s or not words:return []
res = []
one_word = len(words[0])
all_word = len(words) * one_word
words = Counter(words)
length = len(s)
for i in range(0,length - all_word + 1):
tmp = s[i:i + all_word]
now_tmp = []
for j in range(0,all_word,one_word):
now_tmp.append(tmp[j:j + one_word])
if Counter(now_tmp) == words:
res.append(i)
return res

A = Solution()
print(A.findSubstring("barfoothefoobarman",["foo","bar"]))
print(A.findSubstring("ababaab",["ab","ba","ba"]))
posted @ 2020-07-13 19:15  月为暮  阅读(42)  评论(0编辑  收藏