30串聯所有單詞的子串

月為暮發表於2020-07-13
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"]))

相關文章