Find All Anagrams in a String
Easy
Given two strings s and p, return an array of all the start indices of p's anagrams in s.
An anagram is a rearrangement of the characters of a string.
The answer can be returned in any order.
Example 1
Input
s = "cbaebabacd" p = "abc"
Output
[0, 6]
Explanation
The substrings starting at indices 0 and 6 are:
"cba"
"bac"
Both are anagrams of "abc".
Example 2
Input
s = "abab" p = "ab"
Output
[0, 1, 2]
Explanation
The substrings:
"ab"
"ba"
"ab"
are all anagrams of "ab".
Constraints
1 <= s.length
p.length <= 3 * 10^4
s and p consist of lowercase English letters.
Hints:
Hint 1
An anagram has exactly the same character frequencies as the pattern.
Hint 2
Use a frequency array or hash map to store the characters of p.
Maintain a window of size p.length() while traversing s.
Auto
Loading editor...
Input
Expected Output