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

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
相关推荐
Y1nhl14 天前
基础算法:滑动窗口_python版本
开发语言·python·算法·力扣·滑动窗口
漫谈网络14 天前
智能资源管理机制-重传机制
网络·滑动窗口·sdn·数据通信·重传
_extraordinary_17 天前
笔试专题(六)
算法·哈希算法·贪心·模拟·滑动窗口·构造
Repeat71521 天前
日志统计(双指针)
java·数据结构·算法·蓝桥杯·双指针·滑动窗口
破东风21 天前
leetcode每日一题:替换子串得到平衡字符串
算法·leetcode·滑动窗口
ゞ 正在缓冲99%…23 天前
leetcode76.最小覆盖子串
java·算法·leetcode·字符串·双指针·滑动窗口
f狐0狸x1 个月前
【蓝桥杯每日一题】3.28
c语言·数据结构·c++·蓝桥杯·滑动窗口
ゞ 正在缓冲99%…1 个月前
leetcode3.无重复字符的最长字串
算法·leetcode·滑动窗口
小卡皮巴拉1 个月前
【力扣刷题实战】无重复的最长字串
开发语言·c++·算法·leetcode·滑动窗口
daily_23331 个月前
coding ability 展开第四幕(滑动指针——巩固篇)超详细!!!!
c++·算法·字符串·哈希算法·滑动窗口