Ctrl K

Hidden Anagram Windows

Medium
You are given strings text and pattern. Find every position i where text has a stretch of length len(pattern) made of the same letters as pattern, each the same number of times (in other words, a rearrangement of pattern). Return the 0-based start indices of all such stretches in any order. If there are none, return an empty list.
Example 1
text="tealate", pattern="eat" → [0, 4]

The windows of length 3 are "tea" (0), "eal", "ala", "lat", "ate" (4). Only "tea" and "ate" use exactly e, a and t.

Example 2
text="zzzz", pattern="zz" → [0, 1, 2]

Windows may overlap: starts 0, 1 and 2 all match.

Example 3
text="ab", pattern="abc" → []

The pattern is longer than the text, so no window fits.

Constraints
  • 1 ≤ len(text), len(pattern) ≤ 3·10⁴
  • both strings consist of lowercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times Medium
loading Python…
Input
text="tealate", pattern="eat"
Expected
[0, 4]

Run checks the first 3 tests in your browser. Submit checks all 105 tests on the server, hidden ones included.