Problem 365400 · medium · Phase 03 Linear Management & Searching

Scrambled Key Positions

sliding window · fixed-size window · character counts

A secret key may appear inside text with its letters scrambled in any order. Return the list of all start indices i such that text[i:i + len(key)] is a rearrangement of key, in increasing order. Return [] if there are none.

Examples

Input:  text = "cbaebabacd", key = "abc"
Output: [0, 6]
Explanation: "cba" at 0 and "bac" at 6 are rearrangements of "abc".

Input:  text = "abab", key = "ab"
Output: [0, 1, 2]

Constraints

  • 1 <= len(key) <= len(text) <= 10**5
  • Both strings consist of lowercase English letters.
  • Target complexity: O(n) time; sorting or recounting each window is too slow for the largest tests.

Goals

  • Compare letter frequencies of a window with a target in O(1) per step
  • Update a count array as characters enter and leave the window
Starting Python…