Find All Anagrams in a String
Given two strings s and p, return the start indices of all occurrences of p's anagrams in s.
Open on LeetCode ↗Intuition
This is the same fixed-window counting trick as Permutation in String, but two things trip people up here that didn't matter before. First, the problem wants the START INDEX of each matching window, not the window's characters -- if you record the substring itself, you throw away exactly the piece of information the answer needs. Second, it wants EVERY occurrence, including overlapping ones: once a window matches, you don't stop or skip ahead, you just record the index and keep sliding one character at a time, because the very next window (starting one position later) can also be a valid anagram. Keep the same fixed-width window with running counts and a matches counter from Permutation in String, and the only change is what happens on a hit: append the left edge's index to the results list instead of returning immediately.
Approach
Reuse the fixed-window count/matches machinery
Build a need count map from p, then slide a window of width len(p) across s, updating a have map and a matches counter incrementally exactly as in the permutation-in-string pattern -- one character enters, one leaves, per step.
Record the start index, not the window
Whenever matches equals the number of needed keys, the current window (from r - len(p) + 1 to r) is an anagram of p. Append r - len(p) + 1, the window's left edge, to the results list.
Keep sliding after a hit -- never stop or jump
Do not break out of the loop or advance the window by more than one position after a match. Overlapping windows are both valid independently, so the scan must continue exactly as it would after a miss.
Solution & live demo
Edge cases
No window of that width exists, so the results list stays empty; the loop condition naturally prevents any comparison from firing.
Overlapping windows at consecutive start indices are all recorded since the scan never skips ahead after a match.
The need map's counts greater than 1 are handled the same way as unique characters -- matches is keyed on distinct letters reaching their required count, not on total character count.
matches never reaches the needed key count for any window, and the function returns an empty list.