30. 串联所有单词的子串

30. 串联所有单词的子串

  • 一级目录
  • 1. 设置等宽的窗口右移 + 列表对比(超时)
  • 2. 优化遍历方式
  • 3. 字典对比
  • 4. 字典抵消

一级目录

1. 设置等宽的窗口右移 + 列表对比(超时)

超出时间限制

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        # 获取长度
        lw,lww = len(words),len(words[0])
        # 定义窗口右边界
        r = lw*lww
        res = []
        # 遍历
        for i in range(0,len(s)):
            #获取单词字符串
            word = s[i:i+r]
            # 获取单词列表
            li = []
            for j in range(0,len(word),lww):
                li.append(word[j:j+lww])
            if Counter(li)==Counter(words):
                res.append(i)
        return res

2. 优化遍历方式

执行用时:9884 ms, 在所有 Python 提交中击败了5.27%的用户
内存消耗:13.8 MB, 在所有 Python 提交中击败了7.90%的用户
通过测试用例:179 / 179

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        # 获取长度
        lw,lww = len(words),len(words[0])
        # 定义窗口右边界
        r = lw*lww
        ls = len(s)
        res = []
        # 遍历
        for i in range(ls-r+lww):
            #获取单词字符串
            word = s[i:i+r]
            # 获取单词列表
            li = []
            for j in range(0,len(word),lww):
                li.append(word[j:j+lww])
            if Counter(li)==Counter(words):
                res.append(i)
        return res

3. 字典对比

执行用时:2136 ms, 在所有 Python 提交中击败了11.28%的用户
内存消耗:13.7 MB, 在所有 Python 提交中击败了30.83%的用户
通过测试用例:179 / 179

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        # 获取长度
        lw,lww = len(words),len(words[0])
        # 定义窗口右边界
        r = lw*lww
        ls = len(s)
        dic = defaultdict(int)
        for i in words:
            dic[i] += 1
        res = []
        # 遍历所有可能组合
        for i in range(ls-r+lww):
            # 与 words内所有单词字数总和相同的字符串
            w = s[i:i+r]
            # 如果匹配,就把每个单词放入li内,和words进行对比
            d = defaultdict(int)
            for j in range(0,r,lww):
                d[w[j:j+lww]]+=1
            # 当d与dic相同,加入当前i到res
            if d == dic:
                res.append(i)
        return res

4. 字典抵消

执行用时:4184 ms, 在所有 Python 提交中击败了6.77%的用户
内存消耗:13.8 MB, 在所有 Python 提交中击败了35.34%的用户
通过测试用例:179 / 179

class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        # 获取长度
        lw,lww = len(words),len(words[0])
        # 定义窗口右边界
        r = lw*lww
        ls = len(s)
        dic = defaultdict(int)
        for i in words:
            dic[i] += 1
        res = []
        # 遍历所有可能组合
        for i in range(ls-r+lww):
            # 如果匹配,就把每个单词放入li内,和words进行对比
            d = dic.copy()
            flag = True
            for j in range(i,i+r,lww):
                d[s[j:j+lww]]-=1
                if d[s[j:j+lww]] < 0:
                    flag = False
                    break
            # 当d与dic相同,加入当前i到res
            if flag == True:
                res.append(i)
        return res

你可能感兴趣的:(滑动窗口,算法,python,算法)