找到字符串中所有字母异位词【滑动窗口】

Problem: 438. 找到字符串中所有字母异位词

文章目录

思路 & 解题方法

主要需要注意s长度可能比p短

复杂度

时间复杂度:

添加时间复杂度, 示例: O ( 26 n ) O(26n) O(26n)

空间复杂度:

添加空间复杂度, 示例: O ( n ) O(n) O(n)

Code

python 复制代码
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        count_s, count_p = [0] * 26, [0] * 26
        len_s, len_p = len(s), len(p)
        if len_s < len_p:
            return []
        for ch in p:
            count_p[ord(ch) - ord('a')] += 1
        
        for i in range(len_p):
            count_s[ord(s[i]) - ord('a')] += 1
        left, right = 0, len_p - 1
        ans = []
        if count_p == count_s:
            ans.append(left)
        while right < len_s -1:
            count_s[ord(s[left]) - ord('a')] -= 1
            count_s[ord(s[right + 1]) - ord('a')] += 1
            left += 1
            right += 1
            if count_p == count_s:
                ans.append(left)
        return ans
相关推荐
ゞ 正在缓冲99%…2 天前
leetcode438.找到字符串中所有字母异位词
leetcode·滑动窗口
3Cloudream4 天前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等
HUIMU_9 天前
DAY20-新世纪DL(DeepLearning/深度学习)战士:终(目标检测/YOLO)3
深度学习·yolo·目标检测·滑动窗口·非极大值抑制·交并比·bouding box
蜡笔小柯南10 天前
每秒扛住10万请求?RedissonRateLimiter 分布式限流器详解
分布式·redisson·滑动窗口·ratelimiter
Dream it possible!12 天前
LeetCode 面试经典 150_滑动窗口_串联所有单词的子串(32_30_C++_困难)(滑动窗口:控制起点和滑动距离)
c++·leetcode·面试·滑动窗口
Q741_14717 天前
C++ 力扣 76.最小覆盖子串 题解 优选算法 滑动窗口 每日一题
c++·算法·leetcode·双指针·滑动窗口
Q741_14724 天前
C++ 力扣 438.找到字符串中所有字母异位词 题解 优选算法 滑动窗口 每日一题
c++·算法·leetcode·双指针·滑动窗口
Tisfy25 天前
LeetCode 837.新 21 点:动态规划+滑动窗口
数学·算法·leetcode·动态规划·dp·滑动窗口·概率
KarrySmile1 个月前
Day8--滑动窗口与双指针--1004. 最大连续1的个数 III,1658. 将 x 减到 0 的最小操作数,3641. 最长半重复子数组
数据结构·算法·双指针·滑动窗口·不定长滑动窗口·最大连续1的个数·最长子数组
伟大的车尔尼1 个月前
滑动窗口题目:字符串的排列
滑动窗口