Imagine you're a word detective searching for hidden anagrams within a larger string! Given two strings s (the haystack) and p (the needle), your mission is to find all starting positions where anagrams of p appear as substrings in s.
What's an anagram? It's a rearrangement of letters - like "abc" and "bca" are anagrams because they contain the same letters with the same frequency.
Example: If s = "abab" and p = "ab", then positions 0 and 2 contain anagrams of "ab" ("ab" at index 0 and "ab" at index 2).
Return an array of all the starting indices where p's anagrams begin in s. The order of results doesn't matter!
Input & Output
Visualization
Time & Space Complexity
Single pass through string s (n) plus creating frequency map for p (m)
Hash maps store at most 26 characters (constant space for lowercase English letters)
Constraints
- 1 ≤ s.length, p.length ≤ 3 × 104
- s and p consist of lowercase English letters only
- Both strings are non-empty