登录
  • #刷题

Leetcode 30 Substring with Concatenation of All Words 超时了 怎么改进

susu_susu
794
1
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 [][br]
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
1条回复
热度排序

发表回复