You are given two strings, s and p, return all the start indices of p's anagrams in s. You may return the answer in any order.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
Function signature: List[int] findAnagrams(String s, String p)
Input:
s representing the main string (1 <= s.length <= 3 * 10^4)p representing the string which is a possible anagram of some substring of s (1 <= p.length <= s.length)Output:
[a1, a2, ...] containing the starting indices of p's anagrams in s.To solve this problem efficiently, we can use the sliding window technique.
result to store the starting indices of the anagrams.sCount and pCount, of size 26 to represent the count of letters in s and p respectively. Initialize all the elements of both arrays with 0.p and update the pCount array accordingly.left and right, both pointing to the starting position of string s.right pointer reaches the end of string s.right position in string s by 1 in sCount array.right and left pointers is equal to the length of string p, then we have a potential anagram.sCount and pCount arrays. If they are equal, then the current substring from left to right is an anagram of string p. Append the left pointer to the result list.left position in string s by 1 in sCount array.left pointer one step forward.right pointer one step forward.result list containing the starting indices of p's anagrams in s.Let's implement this solution in Python: