Skip to content
Onsite

Find All Anagrams in a String

MediumP2Coding

Given two strings s and p, return the start indices of every substring of s that is an anagram of p (same letters with the same counts, in any order). Return the indices in increasing order.

Example 1

Input
s = "cbaebabacd", p = "abc"
Output
[0,6]

Example 2

Input
s = "abab", p = "ab"
Output
[0,1,2]

Example 3

Input
s = "a", p = "a"
Output
[0]

Constraints

  • 1 <= s.length, p.length <= 3 * 10^4
  • Both strings consist of lowercase English letters.

Hints

Run checks the 3 visible tests, Submit all 11.