leetcode-30-串联所有单词的子串
题目描述: 方法一:暴力O(MN) class Solution: def findSubstring(self, s: str, words: List[str]) -> List[int]: res = [] if not s or not words: return res for j in range(len(s)-len(words[0])*len(words)+1): i = j tmp = words.copy() while i - j <= (len(words)-1)*len(words[0]): if s[i:i+len(words[0])] in tmp: tmp.remove(s[i:i+len(words[0])]) if not tmp: res.append(j) i += len(words[0]) else: break return res 优化: class Solution: def findSubstring(self, s: str, words: List[str]) -> List[int]: from collections import Counter if not s or not words:return [] one_word = len(words[0]) all_len = len(words) * one_word