'''
Author: 365JHWZGo
Description: 30. 串联所有单词的子串
Date: 2021-10-06 10:49:14
FilePath: Pythontestdemo3.py
LastEditTime: 2021-10-06 11:18:45
LastEditors: 365JHWZGo
'''
#思路1:找到words中不同的组合方式,然后判断其每一次在s中出现的位置
#思路2:题中给了条件,每个单词长度相同,可以一次判断是否在其中,然后删除
class Solution:
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
#wordLen 单词长度
#wordsLen 单词数组长度
#result 结果
result = []
wordsTemp = words + []
if words:
wordLen = len(words[0])
wordsLen = len(words)
i = 0
#特殊情况全是一个字母组成的s
if len(set(s))==1 and len(s) == wordsLen:
return [0]
if len(set(s))==1 and len(set(words))==1 and wordLen==1:
return [i for i in range(len(s)-wordLen*wordsLen+1)]
while i < len(s):
for j in range(wordsLen):
part = s[i + j * wordLen:i + j * wordLen + wordLen]
if part in words:
words.remove(part)
else:
break
if len(words) == 0:
result.append(i)
#方框移动
i = i + 1
#恢复原来的words
words = wordsTemp + []
return result
if __name__ == '__main__':
p = Solution()
res = p.findSubstring("a", ["a"])
print(res)