Leetcode:Substring with Concatenation of All Words

下面提供了两种算法解决这个问题:

1: 每遇到一个字母就查找它是不是属于首字母集合,若是则开始解析。解析的时候要注意某个单词的数量不能超过此数。一开始利用递归写的的解析函数超时了,改为循环后成功AC。

class Solution(object):
    def findSubstring(self, s, words):
        if words==[]:return []
        n,k,words_len = len(s),len(words[0]),len(words)
        type_suc,type_fail=1,0
        result,words_,initial = [],{},set()

        def parse(start):
            hash_words = words_.copy()
            while start+k<=n:
                ch = s[start:start+k]
                if hash_words.get(ch,-1)>0:
                    hash_words[ch]-=1
                else:
                    return type_fail
                start+=k
            return type_suc
        def init():
            nonlocal initial,words_
            for word in words:
                initial.add(word[0])
                if word in words_:
                    words_[word]+=1
                else:
                    words_[word] = 1

        start = 0
        init()
        while n-start>=words_len*k:
            if s[start] in initial:
                return_type = parse(start)
                if return_type==type_suc:
                    result.append(start)
                    Next,index = [],0
            start+=1
        return result

2: 这个算法利用了一个已知条件,当我们成功匹配一个序列以后,由于每一个单词长度是k,那么我们可以分析后连续k个字母组成的单词:

[0]如果这个单词是我们要找的单词,且添加这个单词以后新的序列依旧满足条件,那么就可以不停的把序列向右移动k个单位,每次丢掉开头的k个字母。

[1]如果后面的单词不是我们要找的,说明新的序列必须从这个单词以后开始检测。

[2]如果这个单词是集合内的单词,但是出现次数超过限制,那么我们应该丢掉开头的k个字母,将序列右移k(此处有待继续优化)

[3]这个算法效率高的关键就在于利用了每次检测后总有一部分的信息是已经算好的这个条件。可以不用检测所有的信息。类似于,用一个滑动的窗口来保存已经匹配好的信息,然后每次滑动一下,如果新的信息匹配不成功则需要抛弃最左边的信息或者抛弃整个窗口。

class Solution(object):
    def findSubstring(self, s, words):
        def add_ele(word,dic):
            if word not in dic:
                dic[word] = 1
            else:
                dic[word] += 1
        def init():
            nonlocal hash
            for word in words:
                add_ele(word,hash)
        def parse(start,current,indices):
            #start means the successful match start
            #current means the pos of current word 

            hash_word={}
            counter = 0
            while start <= str_len-length:
                first = s[start:start + word_len]
                comparator = s[current:current + word_len]
                word_occurs = hash_word.get(comparator,-1)
                word_occurs_all =hash.get(comparator,-1)

                #not appear
                if word_occurs_all<0:
                    current += word_len
                    counter = 0
                    start = current
                    hash_word = {}
                    continue

                elif word_occurs==word_occurs_all:
                    #word repeat
                    hash_word[first] -= 1
                    start += word_len
                    counter -= 1
                    continue
                else:
                    add_ele(comparator,hash_word)

                counter += 1
                if counter == len(words):
                    indices.append(start)
                    hash_word[first] -= 1
                    start += word_len
                    counter -= 1
                current += word_len

        if s=='' or words == []:
            return []

        indices = []
        hash = {}
        word_len = len(words[0])
        length = len(words) * word_len
        str_len = len(s)
        init()

        for index in range(word_len):
            parse(index,index,indices)
        return indices
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值