bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 438. Find All Anagrams in a String

438. Find All Anagrams in a String

Leetcode Solutions

Sliding Window with Array

  1. Initialize a 26-element array pCount to store the frequency of each character in p.
  2. Initialize a 26-element array sCount to store the frequency of each character in the current window of s.
  3. Initialize an empty list result to store the starting indices of anagrams of p in s.
  4. Populate pCount with the frequency of each character in p.
  5. Populate the first window of sCount with the frequency of each character in the first window of s.
  6. Iterate over the string s with a window of size equal to the length of p: a. If the current window's sCount matches pCount, add the start index of the window to result. b. Move the window one character to the right: decrement the count of the character going out of the window and increment the count of the character coming into the window in sCount.
  7. Return the result list.
UML Thumbnail

Sliding Window with HashMap

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR