题目如下:
题目
通过滑动窗口来取子字符串,并通过字典对象比较单词的出现次数可以求解这个问题。参考代码如下:
class Solution:
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
# 初始化
strlen = 0
res = []
if strlen > len(s) or len(s) == 0 or len(words) == 0:
return res
dic_words = {}
# 转换为字典,并计算出现次数
for w in words:
if w in dic_words:
dic_words[w] += 1
else:
dic_words[w] = 1
# 子串长度
len_word = len(words[0])
len_words = len(words)
strlen += len_word * len_words
j = 0
# 子串滑动窗口
while j + strlen <= len(s):
dic_substr = {}
substr = s[j: j + strlen]
k = 0
# 单词滑动窗口
while k + len_word <= len(substr):
if substr[k: k + len_word] in dic_words:
if substr[k: k + len_word] in dic_substr:
dic_substr[substr[k: k + len_word]] += 1
else:
dic_substr[substr[k: k + len_word]] = 1
k = k + len_word
else:
break
if dic_words == dic_substr:
res.append(j)
j = j + 1
return res
其它题目:leetcode题目答案讲解汇总(Python版 持续更新)
ps:如果您有好的建议,欢迎交流 :-D,
也欢迎访问我的个人博客 苔原带 (www.tundrazone.com)
网友评论