You get two strings, s and p. An anagram of p uses exactly the same letters as p, each one the same number of times, in any order. Find every place in s where a run of p.length consecutive characters is an anagram of p, and return the index where each such run starts.
The runs may overlap. If p is longer than s, nothing fits and the answer is empty. You may return the indices in any order.
tae (index 0), eat (index 2) and eta (index 6) each rearrange into eat. A run such as atb holds a b, so it doesn't count.
Every two-letter run is xy or yx, so all four starting positions count, even though the runs share letters.
p has three letters and s only two, so no run of s is long enough.
Constraints
1 <= s.length <= 3 * 10⁴1 <= p.length <= 3 * 10⁴s and p contain only lowercase English letters.Two runs of the same length are anagrams exactly when every letter appears the same number of times in both, so compare letter counts instead of sorting.
Two runs of letters are anagrams of each other exactly when every letter appears the same number of times in both. Order never matters, so you never need to rearrange anything: you only need 26 counts.
Time O(n + m)Space O(1)
The full walkthrough and code are part of Premium