Coding Interview PatternsFind All Anagrams in a String
MediumTwo Pointers

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"

Output:[0, 6]
0
0
1
6

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⁴
  • s and p consist of lowercase English letters only.

Approach

Two Pointers pattern

1. Handle Edge Case

  • If s is shorter than p, no anagram can exist — return an empty array immediately.

2. Build Frequency Arrays

  • Create two arrays of size 26 (one for each lowercase letter): pCount for p and sCount for the initial window of s.
  • Populate both by iterating through the first p.length characters.

3. Count Initial Matches

  • Compare pCount and sCount at each of the 26 positions.
  • A matches counter 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 equals pCount, increment matches. If it just exceeded pCount, decrement matches.
  • For the removed character: decrement its count in sCount. If the count now equals pCount, increment matches. If it just dropped below pCount, decrement matches.

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 result array containing all start indices where anagrams of p begin in s.

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

Input:
[c, b, a, e, b, a, b, a, c, d], p = abc
c0b1a2e3b4a5b6a7c8d9

Press ▶ or use ← → to step through

Left (L)Right (R)WindowDone
9 steps

Solution Code