python leetcode 30. Substring with Concatenation of All Words

221 篇文章 2 订阅

这里判断是子字符串所以不用最小滑动窗而用DFS也能通过,每次都是从头判断。有几个剪枝点:

1.满足子字符串定长

2.字典中每个字符串定长

3.字典判断,dp1数组判断是否满足跳出循环条件

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        if words==[]: return []
        res=[]
        adict={}
        l=len(words[0])
        count=len(words)
        ls=len(s)
        dp1=[0]*count
        for index in range(count):
            ws=words[index]
            if ws in adict:
                index=adict[ws]
            else:
                adict[ws]=index
            dp1[index]+=1
        for i in range(len(s)-l*count+1):
            curl=i
            dp=dp1[:]
            while curl+l<=ls and s[curl:curl+l] in adict:
                index=adict[s[curl:curl+l]]
                dp[index]-=1
                if dp[index]<0:
                    break 
                curl+=l
            if curl-i==count*l:
                res.append(i)
        return res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值