Find All Anagrams in a String

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Find All Anagrams in a String

Approach

Keep a fixed-size window of length p.length over s. Compare window character frequency to p's frequency at each step and record matching start indices.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Sliding Window (Fixed Size + Frequency)

Why It Works

A substring is an anagram of p exactly when all character counts match. Sliding updates only two character counts per step.

Updated Feb 2026