mediumSliding WindowPattern: Sliding Window / Hash Map

Anagram Index Finder Solution

Problem Statement

Given a string `sequence` and a string `pattern`, find all starting positions in `sequence` where `pattern` or its anagram occurs. Return a list of such positions.

Examples

Example 1:
Input:["abab","ab"]
Output:[0,1,2]
Explanation: The anagrams of 'ab' in 'abab' are at positions 0, 1, and 2

Constraints

  • 1 <= s.length, p.length <= 3 * 10^4
Time: O(N) Space: O(1)
Sliding window with character frequency arrays. If arrays match, increment count. Slide by decrementing outgoing char and incrementing incoming char. Time O(N), Space O(1).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.