leetcode - 438. Find All Anagrams in a String

Description

Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

Constraints:

1 <= s.length, p.length <= 3 * 10^4
s and p consist of lowercase English letters.

Solution

Sliding window, use a hash table to keep track of all the characters we have already visited. If all the characters are visited, then we get our answer.

Time complexity: o ( n ) o(n) o(n)

Space complexity: o ( n ) o(n) o(n)

Code

python3 复制代码
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        available_chars = {}
        for c in p:
            available_chars[c] = available_chars.get(c, 0) + 1
        distinct_chars = len(available_chars)
        left = -1
        res = []
        cur_chars = available_chars.copy()
        for right in range(len(s)):
            if s[right] not in available_chars:
                left = right
                cur_chars = available_chars.copy()
            else:
                while cur_chars[s[right]] <= 0:
                    left += 1
                    cur_chars[s[left]] += 1
                cur_chars[s[right]] -= 1
            if list(cur_chars.values()) == [0] * distinct_chars:
                res.append(left + 1)
        return res

Other method sliding window

python3 复制代码
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        p_chars = {}
        for c in p:
            p_chars[c] = p_chars.get(c, 0) + 1
        s_chars = {}
        left = -1
        res = []
        for right in range(len(s)):
            s_chars[s[right]] = s_chars.get(s[right], 0) + 1
            while len(s_chars) > len(p_chars) or s_chars.get(s[right], 0) > p_chars.get(s[right], 0):
                left += 1
                s_chars[s[left]] -= 1
                if s_chars[s[left]] == 0:
                    s_chars.pop(s[left])
            if s_chars == p_chars:
                res.append(left + 1)
        return res
相关推荐
ChoSeitaku29 分钟前
链表循环及差集相关算法题|判断循环双链表是否对称|两循环单链表合并成循环链表|使双向循环链表有序|单循环链表改双向循环链表|两链表的差集(C)
c语言·算法·链表
DdddJMs__13535 分钟前
C语言 | Leetcode C语言题解之第557题反转字符串中的单词III
c语言·leetcode·题解
Fuxiao___38 分钟前
不使用递归的决策树生成算法
算法
我爱工作&工作love我43 分钟前
1435:【例题3】曲线 一本通 代替三分
c++·算法
白-胖-子1 小时前
【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-统计数字
开发语言·c++·算法·蓝桥杯·等考·13级
workflower1 小时前
数据结构练习题和答案
数据结构·算法·链表·线性回归
好睡凯1 小时前
c++写一个死锁并且自己解锁
开发语言·c++·算法
Sunyanhui12 小时前
力扣 二叉树的直径-543
算法·leetcode·职场和发展
一个不喜欢and不会代码的码农2 小时前
力扣105:从先序和中序序列构造二叉树
数据结构·算法·leetcode
前端郭德纲2 小时前
浏览器是加载ES6模块的?
javascript·算法