Find All Anagrams in a String
Find All Anagrams in a String
Sliding WindowHash Map
Problem Statement
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.
Example
Example 1:
Input: s = "cbaebabacd", p = "abc"
s: "cbaebabacd", p: "abc"
Output: [0, 6]
Output: [0,6]
The substring with start index 0 is "cba", which is an anagram of "abc". The substring with start index 6 is "bac", which is an anagram of "abc".
Solution
This problem can be solved using a sliding window and two hash maps. One map stores the frequency of characters in `p`, and the other stores the frequency of characters in the current window of `s`. We slide the window and compare the maps.
Algorithm Steps
- Create a frequency map for the characters in `p`.
- Initialize a second frequency map for the window in `s`.
- Slide a window of size `p.length()` over `s`.
- For each window, update the window's frequency map.
- If the two frequency maps are identical, add the starting index of the window to the result list.
Find All Anagrams in a String
Sliding Window with Hash Maps
Progress1 / 1
Ready to start the visualization
s
c
b
a
e
b
a
b
a
c
d
p Map
s Window Map
Result
Find All Anagrams in a String Solution