Find All Anagrams in a String
Given strings s and p, return the starting indices of every substring of s that is an anagram of p (same multiset of characters), in any order.
Open official problem prompt ↗Find every position where a fixed-width slice of s is a rearrangement of p.
Slide a stencil of width len(p) along a strip of letters. At each stop you check whether the letters showing through match your target bag of letters, regardless of their order.
- Input
- s = "cbaebabacd", p = "abc"
- Output
- [0, 6]
- Why
- The substring starting at index 0 is "cba" and at index 6 is "bac", both anagrams of "abc".
1 <= s.length, p.length <= 3 * 10^4s and p consist of lowercase English letters