LeetCode-30-Substring with Concatenation of All Words 暴力+Map灵活数据结构

来源:互联网 发布:js获取当前网址 编辑:程序博客网 时间:2024/06/03 20:39


class Solution(object):    def findSubstring(self, s, words):        """        :type s: str        :type words: List[str]        :rtype: List[int]        """        if s=="" or words==[]:return []        wordLen=len(words[0])        Len=len(words)*wordLen        if Len>len(s):return []        Map={}        ans=[]        for i in range(len(words)):            if words[i] not in Map:                Map[words[i]]=(i,1)            else:                Map[words[i]]=(i,Map[words[i]][1]+1)        for l in range(len(s)-Len+1):            cur=[0]*len(words)            flag=0            for j in range(l,l+Len,wordLen):                curstr=s[j:j+wordLen]                if curstr not in Map:                    flag=1                    break;                v=Map[curstr][0]                cur[v]+=1                if cur[v]>Map[curstr][1]:                    flag=1                    break;            if flag==0:                ans.append(l)        return ans