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

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
相关推荐
源代码•宸12 天前
Leetcode—487. 最大连续1的个数 II【中等】Plus
c++·经验分享·算法·leetcode·滑动窗口
神秘的t16 天前
优选算法合集————双指针(专题二)
java·数据结构·算法·滑动窗口
L_M_TY17 天前
G1. Yunli‘s Subarray Queries (easy version)
算法·stl·滑动窗口·离线查询
Dong雨25 天前
力扣hot100-->滑动窗口、贪心
贪心·滑动窗口·力扣hot100
w_outlier1 个月前
TCP__滑动窗口__拥塞控制
网络·网络协议·tcp/ip·滑动窗口·拥塞控制
高 朗1 个月前
【算法刷题】leetcode hot 100 滑动窗口
算法·leetcode·职场和发展·滑动窗口
bohu832 个月前
sentinel学习笔记1-为什么需要服务降级
笔记·学习·sentinel·滑动窗口
幼儿园口算大王2 个月前
关于滑动窗口算法--最小替换字串长度
java·数据结构·算法·滑动窗口
DogDaoDao2 个月前
leetcode 面试经典 150 题:长度最小的子数组
算法·leetcode·面试·双指针·滑动窗口·数据结构与算法·子数组
GGBondlctrl2 个月前
【Leetcode】滑动窗口算法-编程苍穹下划破数据暗夜的高效光弧
数据结构·算法·leetcode·滑动窗口·长度最小子数组·无重复字符长子串