DIY: Find All Anagrams in a String

Solve the interview question "Find All Anagrams in a String" in this lesson.

Problem statement

For this challenge, you are given two non-empty strings, s and p. These strings consist of lowercase English letters only, and the length of both strings will not be larger than 20,100. You have to implement a function that finds all the start indices of p's anagrams in the string s. The order of output does not matter.

Input

The function will take two strings, s and p, as input. The following is an example of the inputs:

s = "aaacbaccbabad" 
p = "abc"

Output

The output will be a list of integers containing the starting indices of anagrams of p in the string s. The following is the output for the inputs given above:

[2, 3, 4, 7]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.