Find every starting index where a substring is an anagram of a given pattern string.
Given two strings s and p, return all starting indices in s where the substring of length |p| is an anagram of p.
A substring is an anagram of p if it uses exactly the same characters with the same frequencies, possibly in a different order.
sps and p typically contain lowercase English lettersExample 1
Input
s = "cbaebabacd", p = "abc"
Output
[0,6]
Explanation
The substrings starting at indices 0 ("cba") and 6 ("bac") are anagrams of "abc".
Example 2
Input
s = "abab", p = "ab"
Output
[0,1,2]
Explanation
The substrings "ab", "ba", and "ab" are all anagrams of "ab".
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.