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
相关推荐
艺术是真的秃头6 分钟前
Trae:当编程从“编写”转向“对话”与“委派”
人工智能·python·ai·aigc
奕成则成11 分钟前
Django使用
后端·python·django
38242782718 分钟前
使用 webdriver-manager配置geckodriver
java·开发语言·数据库·爬虫·python
牛奔21 分钟前
macOS 使用 conda,同时本地安装了python,遇到 ModuleNotFoundError: No module named ‘xxx‘` 解决
开发语言·python·macos·conda
咕白m62523 分钟前
通过 Python 提取 PDF 表格数据(导出为 TXT、Excel 格式)
后端·python
我送炭你添花24 分钟前
Pelco KBD300A 模拟器:01+1.KBD300A 通用键盘详解:DIP 开关与接口配置
python·计算机外设·运维开发
玄同76526 分钟前
Python 项目实战中“高内聚低耦合”的设计方法 —— 基于七大设计原则与拓展技巧
开发语言·人工智能·python·语言模型·pycharm·设计原则·项目实战
iAkuya1 小时前
(leetcode)力扣100 26环状链表2(双指针)
算法·leetcode·链表
sin_hielo1 小时前
leetcode 2402(双堆模拟,小根堆)
数据结构·算法·leetcode
计算机徐师兄1 小时前
Python基于Django的网络入侵检测系统(附源码,文档说明)
python·django·网络入侵检测·网络入侵检测系统·python网络入侵检测系统·网络入侵·python网络入侵检测