Asked at

Find All Anagrams In A String

Medium
Verified
Sliding WindowString MatchingHash Map~25 min

Return the start indices of every substring of s that is an anagram of p, in ascending order.

Slide a fixed window of length p.length across s, comparing letter counts to p’s.

The input arrives as a single object { s, p }.

Examples

in{ s: "cbaebabacd", p: "abc" }
out[0, 6]

"cba" at 0 and "bac" at 6 are anagrams of "abc".

in{ s: "abab", p: "ab" }
out[0, 1, 2]

"ab", "ba", "ab" each match.

Constraints

  • 1 ≤ s.length, p.length ≤ 3·10⁴
  • s and p consist of lowercase English letters.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.