Find All Anagrams in a String

Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.

Example 1
Inputs = "cbaebabacd", p = "abc"
Output[0,6]
The substrings starting at indices 0 and 6 are "cba" and "bac", both of which are anagrams of "abc".
Example 2
Inputs = "abab", p = "ab"
Output[0,1,2]
The substrings starting at indices 0, 1, and 2 are "ab", "ba", and "ab", all of which are anagrams of "ab".

Constraints

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

Asked at 16 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate