Find All Anagrams in a String - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "cbaebabacd", p = "ab"
›
Output:
[1,4,5,6]
💡 Note:
The anagrams of "ab" in "cbaebabacd" start at indices 1 ("ba"), 4 ("ba"), 5 ("ab"), and 6 ("ba"). All of these substrings are anagrams of "ab".
Example 2 — Multiple Matches
$
Input:
s = "abab", p = "ab"
›
Output:
[0,1,2]
💡 Note:
Anagrams start at indices 0 ("ab"), 1 ("ba"), and 2 ("ab"). All substrings of length 2 form anagrams of "ab".
Example 3 — No Matches
$
Input:
s = "xyz", p = "ab"
›
Output:
[]
💡 Note:
No substring of length 2 in "xyz" can form an anagram of "ab" since the characters don't match.
Constraints
- 1 ≤ s.length, p.length ≤ 3 × 104
- s and p consist of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code