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
相关推荐
ζั͡山 ั͡有扶苏 ั͡✾1 天前
从零搭建 Data-Juicer:一站式大模型数据预处理与可视化平台完整教程
python·data-juicer
SkylerHu1 天前
tornado+gunicorn部署设置max_body_size
python·tornado·gunicorn
独行soc1 天前
2025年渗透测试面试题总结-234(题目+回答)
网络·python·安全·web安全·渗透测试·1024程序员节·安全狮
木头左1 天前
年化波动率匹配原则在ETF网格区间选择中的应用
python
清空mega1 天前
从零开始搭建 flask 博客实验(3)
后端·python·flask
程序员小远1 天前
7个常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·测试用例·压力测试·性能测试
红尘炼丹客1 天前
《DeepSeek-OCR: Contexts Optical Compression》速览
人工智能·python·自然语言处理·ocr
☼←安于亥时→❦1 天前
Playwright 安装与使用
python·playwright
sin_hielo1 天前
leetcode 1611
算法·leetcode
大佬,救命!!!1 天前
python实现象棋
开发语言·python·学习笔记·pygame·少儿编程·记录成长