6.22 - hard - 5

30. Substring with Concatenation of All Words

朴素的挨个检查的方法TLE了

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        if not words:
            return []
        l = len(words[0])
        h = {}
        for w in words:
            if w in h:
                h[w] += 1
            else:
                h[w] = 1
        
        res = []
        total_l = l * len(words)
        for i in range(len(s) - total_l+1):
            j = i
            h_copy = copy.deepcopy(h)
            while s[j:j+l] in h_copy:
                if h_copy[s[j:j+l]] >= 1:
                    h_copy[s[j:j+l]] -= 1
                else:
                    break
                j += l
                if j - i == total_l:
                    res.append(i)
        return res

然后看了答案,仔仔细细把这道题又重写了一遍,发现其实就是一道dynamic window的问题,自己仔细的体会了一下,感觉对这种dynamic window的题目又加深了不少的理解

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        result = []
        if not words or len(s) < len(words) * len(words[0]):
            return result
        wl, total_length, n, word_dict = len(words[0]), len(words)*len(words[0]), len(s), {}
        # init word_dict
        for w in words:
            word_dict[w] = word_dict.get(w, 0) + 1
        
        # loop i here mean if there is a reslut index, then the result index must be i + m*wl, m is [0,1,2,3,4...]
        for i in range(wl):
            # it is a dynamic length window problem here
            # init the left boundary and tmp_dict
            left, tmp_dict = i, {} 
            for j in range(i, n-wl+1, wl): 
                # for each string, right boundary is fixed j+wl, only thing to consider is to shrink left boundary or not
                str = s[j: j+wl]
                right = j+wl
                if word_dict.get(str):
                    tmp_dict[str] = tmp_dict.get(str,0) + 1
                    # if current tmp_dict is not valid, keep moving the left boundary until it is valid
                    while tmp_dict[str] > word_dict[str]:
                        tmp_dict[s[left: left+wl]] -= 1 
                        left += wl                       
                    
                    # if the window length equals to result length
                    if right - left == total_length:
                        result.append(left)
                        # shrink the window by moving the left to right with wl 
                        tmp_dict[s[left: left+wl]] -= 1
                        left += wl
                else:
                    # if current str not in word_dict, then move left to next j
                    left, tmp_dict = j+wl,{}
        return result
                
                
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,642评论 18 399
  • 每天总结hard20题,三段总解法:1. 找个比较规范的答案,2.把每一行的思路写下来,3.删掉答案重写一遍。不过...
    健时总向乱中忙阅读 1,589评论 0 0
  • 洪水越涨越高,小镇快被淹没,大家都很发愁,有个孩子说:魔术师,让我躲到你帽子里吧!魔术师觉得主意不错,就像装兔子一...
    洞庭府君阅读 2,773评论 0 3
  • 谁说结婚是一辈子的大事,分明是把自己的每一天过好才是人生大事。 爱的前提是独立。
    与灵魂沟通阅读 1,153评论 0 0

友情链接更多精彩内容