412. Find All Anagrams via Sliding Window

HardLinked ListSliding WindowHashString

Given a string s and a pattern p (each viewed as a linked list of characters), return the starting indices of all substrings of s that are anagrams of p, in ascending order. Use a fixed-size sliding window with character counts. The input is JSON {s, p}. Return the indices as an array.

Input: JSON {s, p}.

Output: Array — the anagram start indices.

Examples

Example 1
Input: {"s":"cbaebabacd","p":"abc"}
Output: [0,6]
Explanation: 'cba' at 0 and 'bac' at 6.
Example 2
Input: {"s":"abab","p":"ab"}
Output: [0,1,2]
Explanation: Three anagram windows.
Example 3
Input: {"s":"aa","p":"bb"}
Output: []
Explanation: No anagrams.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →