LeetCode热题100-找到字符串中所有字母异位词

给定两个字符串 sp,找到 s中所有 p异位词的子串,返回这些子串的起始索引。不考虑答案输出的顺序。

通过阅读题目首先想到的就是通过排序判断是否为异位词子串,但是这提交会出超时,时间复杂度较高(O (n×m log m)

python 复制代码
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        if not s:
            return []
        
        def isequal(s1: str, s2: str):
            s1_tmp = sorted(s1)
            s2_tmp = sorted(s2)
            return s1_tmp == s2_tmp
        
        res = []
        length = len(p)
        i = 0
        while i < len(s):
            right = i + length -1
            if right + 1 <= len(s) and isequal(s[i:right+1], p):
                res.append(i)
            i += 1
        
        return res

这里使用滑动窗口 + 哈希表计数,因为只需要判断字符个数一致即为异位词,所以只需要使用哈希表保证窗口内和给定p哈希表一致就说明一样,这里使用普通的字典,使用defaultdict比较方便,因为会自动初始化为0,省去判断字典是否存在。

python 复制代码
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        len_s = len(s)
        len_p = len(p)
        if len_s < len_p:
            return []
        
        p_dict, w_dict = {}, {}
        for c in p:
            if c in p_dict:
                p_dict[c] += 1
                continue
            p_dict[c] = 1
        
        for c in s[:len_p]:
            if c in w_dict:
                w_dict[c] += 1
                continue
            w_dict[c] = 1
        
        res = []
        if w_dict == p_dict:
            res.append(0)
        
        for i in range(len_p, len_s):
            left_char = s[i - len_p]
            w_dict[left_char] -= 1
            if w_dict[left_char] == 0:
                del w_dict[left_char]
            
            right_char = s[i]
            if right_char in w_dict:
                w_dict[right_char] += 1
            else:
                w_dict[right_char] = 1
            
            if w_dict == p_dict:
                res.append(i - len_p + 1)
        
        return res
python 复制代码
from collections import defaultdict

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        len_s = len(s)
        len_p = len(p)
        if len_s < len_p:
            return []
        
        p_dict, w_dict = defaultdict(int), defaultdict(int)
        for c in p:
            p_dict[c] += 1
        
        for c in s[:len_p]:
           w_dict[c] += 1
        
        res = []
        if w_dict == p_dict:
            res.append(0)
        
        for i in range(len_p, len_s):
            left_char = s[i - len_p]
            w_dict[left_char] -= 1
            if w_dict[left_char] == 0:
                del w_dict[left_char]
            
            right_char = s[i]
            w_dict[right_char] += 1
            
            if w_dict == p_dict:
                res.append(i - len_p + 1)
        
        return res
相关推荐
sali-tec几秒前
C# 基于OpenCv的视觉工作流-章108-区域筛选
图像处理·人工智能·opencv·算法·计算机视觉
Gigavision6 小时前
基于BUAA-MIHR数据集的噪声解耦对比学习算法
人工智能·python·深度学习·算法
鹿角片ljp6 小时前
LeetCode 64:最小路径和复盘|二维 DP 与 ACM 模式完整写法
java·数据结构·算法
彧azz7 小时前
算法设计与分析:贪心与动态规划
数据结构·学习·算法·贪心算法·动态规划
Beyond_System|系统之外8 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
漂流瓶jz10 小时前
UVA-1442 洞穴 题解答案代码 算法竞赛入门经典第二版
c++·算法·题解·aoapc·算法竞赛入门经典·uva
圣保罗的大教堂10 小时前
leetcode 835. 图像重叠 中等
leetcode
叠层归一研究院11 小时前
基于极限自指的叠层归一宇宙结构理论——无元外部封闭系统的内生区分模型
人工智能·经验分享·算法·agi
Selvaggia12 小时前
Diffusion Model 的基本原理与模型架构
算法
wabs66613 小时前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树