注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Leetcode 30 Substring with Concatenation of All Words
我的解法超时了 求帮看怎么改进
class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
if not words:
return []
n = len(words[0])
result = list()
need = collections.Counter(words)
for start in range(0, len(s)-n*len(words)+1):
found = collections.Counter()
miss = len(words)
for end in range(start, len(s)-n+1, n):
word = s[end:end+n]
if need[word] <= found[word]:
break
found[word] += 1
miss -= 1
if miss == 0:
result.append(start)
break
return result |