题解 | #牛牛的串联子串游戏#
牛牛的串联子串游戏
https://www.nowcoder.com/practice/c1984371372b43f3b10bf6d0231520bb
class Solution:
def findSubstring(self , s: str, words: List[str]) -> List[int]:
# write code here
ans, wordLen = [], len(words[0])
cnt = dict()
for i, word in enumerate(words):
cnt[word] = i
strs = []
for index in range(0, len(s), wordLen):
if s[index:index+wordLen] in words:
strs.append(cnt[s[index:index+wordLen]])
else:
strs.append(-1)
seen = []
for i, ss in enumerate(strs):
if ss == -1:
if len(seen) == len(words):
ans.append(i - len(words))
seen.clear()
continue
if ss not in seen:
seen.append(ss)
else:
if len(seen) == len(words):
ans.append(i - len(words))
while seen and ss in seen:
seen.pop(0)
if seen and len(seen) == len(words):
ans.append(len(words) - len(seen))
for i, a in enumerate(ans):
ans[i] = a * wordLen
return ans
滑动窗口