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
相关推荐
爱学习的徐徐9 分钟前
Python 豆瓣TOP250 爬虫类讲解
爬虫·python
老歌老听老掉牙12 分钟前
SymPy 绘图完全指南:从基础到高级应用
python·绘图·sympy
小白学大数据17 分钟前
Python爬虫技术:招标信息抓取与关键词过滤 (1)
开发语言·爬虫·python
电商API_180079052471 小时前
获取淘宝商品视频API接口解析:通过商品链接url获取商品视频item_video
开发语言·爬虫·python·数据挖掘·数据分析
精灵vector1 小时前
构建自定义AI客户支持助手——LangGraph 中断机制
人工智能·python
用户8356290780512 小时前
使用Python自动化移除Excel公式,保留纯净数值
后端·python
Pocker_Spades_A2 小时前
Python快速入门专业版(五十):Python异常处理:try-except语句(捕获单一与多个异常)
开发语言·python
Gerlat小智2 小时前
【Python精讲 16】实战项目演练(二):用Flask/FastAPI发布你的第一个Web API
python·flask·fastapi
fenghx2583 小时前
vscode使用arcpy-选择arcgis带的python+运行错误解决
vscode·python·arcgis
王嘉俊9253 小时前
Flask 入门:轻量级 Python Web 框架的快速上手
开发语言·前端·后端·python·flask·入门