visual walkthrough
Find All Anagrams in a String
Same as Permutation in String but collecting every match.
The idea
Same fixed-size window as "permutation in string", but collect every start that matches instead of stopping at the first.
Counting letters and updating the counts as the window slides makes each step O(1).
Complexity
| approach | time | space |
|---|---|---|
| Sort every window | O(n · m log m) | O(m) |
| Slide with counts | O(n) | O(1) |