#438

Find All Anagrams in a String

Medium Fixed window + counts

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return every starting index in s where a rearrangement of p begins.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: every start index where a rearrangement of p begins in s.

Why this works: same counting window as above, but record every match.

Example: s = "cbaebabacd", p = "abc"

index 0 "cba" ✓index 6 "bac" ✓answer [0, 6]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Recording the end index of the window instead of the start.

Solve on LeetCode ↗
Complexity
  • Time: O(26 · n).
  • Space: O(26).
Topic: Sliding Window
🎬 Episode 10: video coming soon