LeetCode题解(0438):找到字符串中所有字母异位词(Python)
发布日期:2021-06-29 20:09:21 浏览次数:3 分类:技术文章

本文共 738 字,大约阅读时间需要 2 分钟。

题目:(中等)

标签:哈希表

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( S + P ) O(S+P) O(S+P) O ( P ) O(P) O(P) 160ms (35.34%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def findAnagrams(self, s: str, p: str) -> List[int]:        size1, size2 = len(s), len(p)        if size1 < size2:            return []        ans = []        aim = collections.Counter(p)        count = collections.Counter()        for i in range(size2):            count[s[i]] += 1        if count == aim:            ans.append(0)        for i in range(size1 - size2):            count[s[i]] -= 1            if count[s[i]] == 0:                del count[s[i]]            count[s[i + size2]] += 1            if count == aim:                ans.append(i + 1)        return ans

转载地址:https://dataartist.blog.csdn.net/article/details/109836410 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(0438):四数相加II(Python)
下一篇:LeetCode题解(0380):常数时间插入、删除和获取随机元素(Python)

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月09日 00时53分29秒