Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-181

Find All Anagrams in a String

Problem

Return the sorted starting indices of every substring of s that is an anagram of p.

The window length is fixed at len(p): slide it forward one character at a time, updating a frequency table and recording the start whenever the table matches p's counts. Return an empty list for an empty pattern.

Hint

Delete zero entries from the window table so it compares equal to the need table.

Test cases

case 1
in: ["cbaebabacd","abc"]
expected: [0,6]
case 2
in: ["abab","ab"]
expected: [0,1,2]
case 3
in: ["a","a"]
expected: [0]
case 4
in: ["","a"]
expected: []
case 5
in: ["abc","d"]
expected: []
Starter codePython
def find_anagram_indices(s, p):
    # Your code here
    pass

5 test cases · run your code against them in the editor