Python | Leetcode Python题解之第30题串联所有单词的子串

题目:

题解:

python 复制代码
class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        res = []
        m, n, ls = len(words), len(words[0]), len(s)
        for i in range(n):
            if i + m * n > ls:
                break
            differ = Counter()
            for j in range(m):
                word = s[i + j * n: i + (j + 1) * n]
                differ[word] += 1
            for word in words:
                differ[word] -= 1
                if differ[word] == 0:
                    del differ[word]
            for start in range(i, ls - m * n + 1, n):
                if start != i:
                    word = s[start + (m - 1) * n: start + m * n]
                    differ[word] += 1
                    if differ[word] == 0:
                        del differ[word]
                    word = s[start - n: start]
                    differ[word] -= 1
                    if differ[word] == 0:
                        del differ[word]
                if len(differ) == 0:
                    res.append(start)
        return res
相关推荐
夏鹏今天学习了吗33 分钟前
【LeetCode热题100(95/100)】寻找重复数
算法·leetcode·职场和发展
火云洞红孩儿4 小时前
告别界面孤岛:PyMe如何用一站式流程重塑Python GUI开发?
开发语言·python
攻城狮7号4 小时前
不懂代码也能造?TRAE+GLM-4.6 手把手教你搭心理咨询智能客服小程序
python·小程序·uni-app·vue·trae·glm我的编程搭子·glm-4.6
叫我辉哥e14 小时前
新手进阶Python:办公看板集成ERP跨系统同步+自动备份+AI异常复盘
开发语言·人工智能·python
圣保罗的大教堂5 小时前
leetcode 3315. 构造最小位运算数组 II 中等
leetcode
布局呆星5 小时前
闭包与装饰器
开发语言·python
全栈测试笔记5 小时前
异步函数与异步生成器
linux·服务器·前端·数据库·python
木头左5 小时前
基于Backtrader框架的指数期权备兑策略实现与分析
python
Anastasiozzzz5 小时前
leetcode力扣hot100困难题--4.俩个正序数列的中位数
java·算法·leetcode·面试·职场和发展
素心如月桠6 小时前
cmd 输入 python --version 输出为空(windows11系统安装python后执行python --version没反应)
python