整体思路就是先切割再对比。
import re
def cut_text(text, length):
res = re.findall('.{'+str(length)+'}', text)
res.append(text[(len(res)*length):])
return res
class Solution:
def findSubstring(self, s, words):
res = []
if words:
l1 = len(words[0])
l2 = len(words)
i = 0
else:
return res
if l1 * l2 > len(s):
return res
else:
while i <= len(s) - l1 * l2:
if s[i:i+l1] in words:
s2 = s[i:i+(l2*l1)]
s3 = sorted(cut_text(s2, l1)[:-1])
words_tmp = sorted(words)
if s3 == words_tmp:
res.append(i)
i += 1
else:
i += 1
else:
i += 1
return res