[LC] 438. Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

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".

Time: O(N)
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:

        my_dict = {}
        res = []
        count = 0
        for char in p:
            freq = my_dict.get(char, 0)
            my_dict[char] = freq + 1

        for i in range(len(s)):
            char = s[i]
            if char in my_dict:
                my_dict[char] -= 1
                if my_dict[char] == 0:
                    count += 1

            if i >= len(p):
                start = i - len(p)
                start_char = s[start]
                if start_char in my_dict:
                    my_dict[start_char] += 1
                    if my_dict[start_char] == 1:
                        count -= 1
            # check my_dict size instead of len(p)
            if count == len(my_dict):
                res.append(i - len(p) + 1)
        return res

原文地址:https://www.cnblogs.com/xuanlu/p/11653258.html

时间: 2024-10-31 19:25:46

[LC] 438. Find All Anagrams in a String的相关文章

438. Find All Anagrams in a String

Problem statement Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of outpu

Leetcode 438. Find All Anagrams in s String

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.

Leetcode——438. Find All Anagrams in a String【java】

第一反应感觉是排列组合及字符匹配问题 [既不是可重复排列,有可能不是全排列] Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,10

letecode [438] - Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.

【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词

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&

[leetcode]438. Find All Anagrams in a String找出所有变位词

如题 思路: 代码: 1 class Solution { 2 public List<Integer> findAnagrams(String s, String p) { 3 //corner case 4 List<Integer> res = new ArrayList<>(); 5 if (s == null || s.length() == 0 || p == null || p.length() == 0) return res; 6 // initial

[leetcode] Valid Anagram 、 Find All Anagrams in a String

Valid Anagram Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Note:

leetcode 438. 找到字符串中所有字母异位词(Find All Anagrams in a String)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100. 说明: 字母异位词指字母相同,但排列不同的字符串. 不考虑答案输出的顺序. 示例 1: 输入: s: "cbaebabacd" p: "abc" 输出: [0, 6] 解释: 起始索引等于 0 的子串是 "

[LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.