Search topics...

Find All Anagrams in a String

medium
stringTime: O(Ns + Np)Space: O(1)Frequency: 4

Given two strings s and p, find all start indices of p's anagrams in s.

Example 1:

Input: s = "cbaebabacd", p = "abc"
Output: [0, 6]
Explanation: "cba" at index 0 and "bac" at index 6 are anagrams of "abc".

Example 2:

Input: s = "abab", p = "ab"
Output: [0, 1, 2]
hash-map