Problem Statement

Find All Anagrams in a String

You get two strings, s and p. You want to find every place in s where the letters of p show up, just shuffled into a different order. Each such place is marked by the index where it starts. Return a list of all those start indices, in any order. An anagram is a word made by rearranging the letters of another word. So "abc", "bca", and "cab" are all anagrams of each other, because they use exactly the same letters the same number of times.

easySliding WindowSliding WindowTime: O(n) · Space: O(1)

Signals to notice

find all starting indices where an anagram of p exists in sfixed-size substring matchingcharacter frequency comparison

Brute force first

For each position in s, extract a substring of length p and sort both to compare. The redundancy is that you're re-sorting from scratch at every position instead of updating incrementally. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

Slide a window of exactly len(p) over s, maintaining a frequency map. When the window's character counts match p's counts, record the index. Each slide adds one character and removes one — total. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Trace it on s="cbaebabacd", p="abc" (len(p)=3)

init: p_count={a:1,b:1,c:1}; window=Counter("cba")={c:1,b:1,a:1} == p_count -> result=[0]
i=3 s[3]='e': +e, -s[0]='c'(=0,del) -> window={b:1,a:1,e:1} != p_count
i=4 s[4]='b': +b(=2), -s[1]='b'(=1) -> window={b:1,a:1,e:1} != p_count
i=5 s[5]='a': +a(=2), -s[2]='a'(=1) -> window={b:1,a:1,e:1} != p_count
i=6 s[6]='b': +b(=2), -s[3]='e'(=0,del) -> window={b:2,a:1} != p_count
i=7 s[7]='a': +a(=2), -s[4]='b'(=1) -> window={b:1,a:2} != p_count
i=8 s[8]='c': +c, -s[5]='a'(=1) -> window={b:1,a:1,c:1} == p_count -> append 8-3+1=6 -> result=[0,6]
i=9 s[9]='d': +d, -s[6]='b'(=0,del) -> window={a:1,c:1,d:1} != p_count; loop ends -> return [0,6]

What must stay true

The window always contains exactly len(p) characters. If its frequency map equals p's frequency map, we've found an anagram. The window never grows or shrinks beyond this size. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Shape of the loop

p_count = Counter(p)
window  = Counter(s[:len(p)])          # first fixed-size window
result  = [0] if window == p_count else []
for i in range(len(p), len(s)):        # slide one char at a time
    window[s[i]] += 1                  # add entering char
    window[s[i-len(p)]] -= 1           # drop leaving char (del if 0)
    if window == p_count: result.append(i - len(p) + 1)
return result

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Comparing entire frequency maps at every position is per step — still fast, but the smarter approach tracks a 'matches' counter that updates in. The real trap is forgetting that the window is fixed-size, not variable. The fix is usually to return to the meaning of each move, not just the steps themselves.

Sliding Window Pattern