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

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
相关推荐
minhuan9 小时前
大模型应用:稀疏注意力 vs 滑动窗口:大模型扩窗技术完全解析.58
滑动窗口·大模型应用·稀疏注意力·大模型扩窗技术
老鼠只爱大米4 天前
LeetCode算法题详解 76:最小覆盖子串
算法·leetcode·双指针·滑动窗口·最小覆盖子串·minwindow
老鼠只爱大米4 天前
LeetCode算法题详解 239:滑动窗口最大值
算法·leetcode·双端队列·滑动窗口·滑动窗口最大值·单调队列
老鼠只爱大米6 天前
LeetCode算法题详解 3:无重复字符的最长子串
算法·leetcode·面试题·滑动窗口·无重复字符的最长子串·最长子串
童话名剑10 天前
目标检测(吴恩达深度学习笔记)
人工智能·目标检测·滑动窗口·目标定位·yolo算法·特征点检测
阿巴~阿巴~14 天前
从滑动窗口到拥塞控制:TCP高效可靠传输的三大支柱
服务器·网络·网络协议·tcp·滑动窗口·流量控制·拥塞控制
CodeAmaz17 天前
常用限流算法详解
滑动窗口·限流算法·令牌桶
Espresso Macchiato20 天前
Leetcode 3768. Minimum Inversion Count in Subarrays of Fixed Length
滑动窗口·leetcode hard·leetcode双周赛171·leetcode 3768
weixin_4617694021 天前
3. 无重复字符的最长子串
c++·算法·滑动窗口·最长字串
nju_spy22 天前
12月力扣每日一题(划分dp + 单调栈 + 堆 + 会议安排)
算法·leetcode·二分查找·动态规划·滑动窗口·单调栈·最大堆