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
相关推荐
GinoWi2 分钟前
Chapter 6 Python中的字典
python
zh路西法6 分钟前
【宇树机器人强化学习】(七):复杂地形的生成与训练
python·深度学习·机器学习·机器人
python猿11 分钟前
打卡Python王者归来--第30天
开发语言·python
2401_8318249626 分钟前
为你的Python脚本添加图形界面(GUI)
jvm·数据库·python
2401_8796938728 分钟前
用Pygame开发你的第一个小游戏
jvm·数据库·python
用户03321266636735 分钟前
使用 Python 查找并高亮 Word 文档中的文本
python
xushichao198937 分钟前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python
yy我不解释1 小时前
关于comfyui的mmaudio音频生成插件时时间不一致问题(三)
开发语言·python·ai作画·音视频·comfyui
冗量1 小时前
langchain的学习路径
python·langchain
love530love1 小时前
不用聊天软件 OpenClaw 手机浏览器远程访问控制:Tailscale 配置、设备配对与常见问题全解
人工智能·windows·python·智能手机·tailscale·openclaw·远程访问控制