Find All Anagrams in a String
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
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.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 27, 2026
Last updated
Aug 27, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output