Find All Anagrams in a String

Given two strings *s* and *p*, return *an array of all the start indices of p's anagrams in s*. You may return the answer in any order. An anagram of p is a permutation of p's letters.

Input Format

s = "text string", p = "pattern"

Output Format

int[] (list of starting indices)

Constraints

  • 1 <= s.length, p.length <= 3 * 10^4; s and p consist of lowercase letters.

Examples

Example 1:

Input:

s = "cbaebabacd"
p = "abc"

Output:

[0,6]

Explanation:

"abc" at index 0, "bac" at index 6 are anagrams of p.

Example 2:

Input:

s = "abab"
p = "ab"

Output:

[0,1,2]

Explanation:

"ab" at 0, "ba" at 1, "ab" at 2 are all anagrams of p.

Loading...
Find All Anagrams in a String - Hashing