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

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
相关推荐
GEEK零零七7 天前
Leetcode 159. 至多包含两个不同字符的最长子串
算法·leetcode·滑动窗口
卷卷的小趴菜学编程1 个月前
算法篇-----滑动窗口
数据结构·算法·双指针·滑动窗口·哈希表·数组相关
满怀10151 个月前
【数据通信完全指南】从物理层到协议栈的深度解析
5g·滑动窗口·数据封装·crc校验·qam调制
阳洞洞1 个月前
滑动窗口leetcode 209和76
算法·leetcode·双指针·滑动窗口
2401_858286111 个月前
CC52.【C++ Cont】滑动窗口
开发语言·数据结构·c++·算法·leetcode·滑动窗口
阳洞洞1 个月前
leetcode 2516. 每种字符至少取 K 个
算法·leetcode·滑动窗口
Y1nhl2 个月前
基础算法:滑动窗口_python版本
开发语言·python·算法·力扣·滑动窗口
漫谈网络2 个月前
智能资源管理机制-重传机制
网络·滑动窗口·sdn·数据通信·重传
_extraordinary_2 个月前
笔试专题(六)
算法·哈希算法·贪心·模拟·滑动窗口·构造
Repeat7152 个月前
日志统计(双指针)
java·数据结构·算法·蓝桥杯·双指针·滑动窗口