Search topics...
All Problems

Find All Anagrams in a String

Solution Approach
Was this helpful?
MediumstringExpected: O(Ns + Np) time, O(1) space
hash-map

Problem

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]
Reference solution unlocks after your first submission
Loading...