Skip to the code
cf.
+25 XP

#144 Scrambled Keyword Positions

medium

A word game highlights every place in a line of text text where the letters of a keyword word appear in any order: a slice of text with exactly the same letters as word, each the same number of times.

Write scrambledPositions(text, word) that returns the start index of every such slice, in ascending order. Slices may overlap.

Example 1

Input: text = "tabbatcab", word = "bat"
Output: [0,3]

tab starts at 0 and bat starts at 3.

Example 2

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

ab, ba, and ab overlap.

Constraints

  • 1 <= text.length, word.length <= 30000
  • Both strings contain lowercase letters only.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).