← Back to challenges

Find All Anagrams in a String

PythonHardalgorithmslogicstrings

Instructions

An anagram is a word formed by rearranging the letters of a different word using all the original letters exactly once.

The function is given two strings: s - to search in, p - a template word. Find the starting indexes of anagrams of p among substrings of the given s.

Examples

find_anagrams("cbaebabacd", "abc") ➞ [0, 6]
# Anagrams: "cba", "bac"

find_anagrams("abab", "ab") ➞ [0, 1, 2]
# Anagrams: "ab", "ba", "ab"

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.