Gamified challenge · Fixed sliding window
text and word are made of lowercase letters a-z. A position i is a hit if the len(word) letters starting at i are a rearrangement of word: the same letters, each the same number of times, in any order.
Write findAnagrams(text, word) that returns every hit position, in any order. For text = "cbaebabacd" and word = "abc" the answer is [0, 6] (cba and bac). If the word is longer than the text, return an empty list.
Sorting every window works but is slow; keep 26 letter counts for the window and update them as it slides.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google