leetcode——30. 串联所有单词的子串

class Solution:
    def findSubstring(self, s: str, words):
        if s=='' or words==[]:return []
        n=len(words)
        m=len(words[0])
        r=[]
        i=0
        while i<len(s)-m*n+1:
            for j in words:
                p=s[i:i+m*n]
                t=list(p[m*i:m*(i+1)] for i in range(n))
                if t.count(j)!=words.count(j):
                    i+=1
                    break
            else:
                r.append(i)
                i+=1
        return r
执行用时 :908 ms, 在所有 python3 提交中击败了48.75%的用户
内存消耗 :13.9 MB, 在所有 python3 提交中击败了5.30%的用户
 
这里面用到 t 的原因是:
不用 t 的时候,比如:s = "ababaab", words = ["ab","ba","ba"] 就会报错!
错误原因:因为计算时候我们会从字符串中间计算,也就是说会出现单词截断的问题。
 
 
 
posted @ 2019-10-18 17:15  欣姐姐  阅读(139)  评论(0编辑  收藏  举报