← Back to challenges

Find All Anagrams in a String

JavaScriptHardalgorithmslogicstrings

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

findAnagrams("cbaebabacd", "abc") ➞ [0, 6]
// Anagrams: "cba", "bac"

findAnagrams("abab", "ab") ➞ [0, 1, 2]
// Anagrams: "ab", "ba", "ab"

Notes

N/A

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