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
相关推荐
这里有鱼汤19 分钟前
📊量化实战篇:如何计算RSI指标的“拥挤度指标”?
后端·python
JJJJ_iii22 分钟前
【机器学习05】神经网络、模型表示、前向传播、TensorFlow实现
人工智能·pytorch·python·深度学习·神经网络·机器学习·tensorflow
William.csj25 分钟前
服务器/Pytorch——对于只调用一次的函数初始化,放在for训练外面和里面的差异
人工智能·pytorch·python
夏鹏今天学习了吗26 分钟前
【LeetCode热题100(47/100)】路径总和 III
算法·leetcode·职场和发展
Ingsuifon28 分钟前
pytorch踩坑记录
人工智能·pytorch·python
smj2302_7968265231 分钟前
解决leetcode第3721题最长平衡子数组II
python·算法·leetcode
m0_626535201 小时前
力扣题目练习 换水问题
python·算法·leetcode
一匹电信狗1 小时前
【LeetCode_160】相交链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl
软件技术NINI1 小时前
MATLAB疑难诊疗:从调试到优化的全攻略
javascript·css·python·html
Q_Q19632884751 小时前
python+uniapp基于微信小程序的助眠小程序
spring boot·python·小程序·django·flask·uni-app·node.js