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
相关推荐
那雨倾城13 分钟前
PiscCode使用OpenCV实现漂浮方块特效
python·opencv
awonw13 分钟前
[python][flask]Flask-Principal 使用详解
开发语言·python·flask
赵英英俊2 小时前
Python day26
开发语言·python
你怎么知道我是队长2 小时前
python---eval函数
开发语言·javascript·python
Rockson2 小时前
期货实时行情接口接入教程
python·api
awonw3 小时前
[python][基础]Flask 技术栈
开发语言·python·flask
bright_colo4 小时前
Python-初学openCV——图像预处理(四)——滤波器
python·opencv·计算机视觉
Nandeska4 小时前
一、Python环境、Jupyter与Pycharm
python·jupyter·pycharm
技术卷4 小时前
详解力扣高频SQL50题之610. 判断三角形【简单】
sql·leetcode·oracle