Find All Anagrams in a String
Explanation & Solution
Description
Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.
An anagram is a rearrangement of all characters in a string.
Input: s = "cbaebabacd", p = "abc"
Explanation: The substring starting at index 0 is "cba", which is an anagram of "abc". The substring starting at index 6 is "bac", which is also an anagram of "abc".
Constraints
1 <= s.length, p.length <= 3 * 10⁴sandpconsist of lowercase English letters only.
Approach
Two Pointers pattern
1. Handle Edge Case
- If
sis shorter thanp, no anagram can exist — return an empty array immediately.
2. Build Frequency Arrays
- Create two arrays of size 26 (one for each lowercase letter):
pCountforpandsCountfor the initial window ofs. - Populate both by iterating through the first
p.lengthcharacters.
3. Count Initial Matches
- Compare
pCountandsCountat each of the 26 positions. - A
matchescounter tracks how many character frequencies are equal between the two arrays. - When
matches === 26, every character count is identical, meaning the current window is an anagram.
4. Slide the Window
- Move the window one position to the right by adding the next character and removing the leftmost character.
- For the added character: increment its count in
sCount. If the count now equalspCount, incrementmatches. If it just exceededpCount, decrementmatches. - For the removed character: decrement its count in
sCount. If the count now equalspCount, incrementmatches. If it just dropped belowpCount, decrementmatches.
5. Record Anagram Indices
- Before each slide, check if
matches === 26. If so, the current window start index is an anagram start — push it to the result. - After the loop ends, check the final window position as well.
6. Return the Result
- Return the
resultarray containing all start indices where anagrams ofpbegin ins.
Key Insight
Instead of comparing full frequency arrays on every slide (O(26) per step), we maintain a matches counter that tracks how many of the 26 character slots have equal counts. Each slide only affects two characters, so updates are O(1). This gives us an overall O(n) time complexity with O(1) space (the two fixed-size arrays).
Visualization
Press ▶ or use ← → to step through