DIY: Find All Anagrams in a String

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

We'll cover the following

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.


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

s = "aaacbaccbabad" 
p = "abc"


The output will be a array 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 80+ hands-on prep courses.