Find All Anagrams of a Pattern in a String
At Amazon, efficient text processing is crucial for services like search indexing, content moderation, and fraud detection. One common task is identifying patterns within large text bodies. This problem challenges you to find all occurrences of a given pattern's anagrams within a larger string. Your solution will contribute to ensuring fast and accurate pattern matching, a key component in maintaining high-quality service performance.
Implement a function findAllAnagrams that identifies all starting indices in a given string s where an anagram of another string p exists.
s (the main text) and p (the pattern).s where an anagram of p is found.s: A string representing the main text to search within.p: A string representing the pattern whose anagrams we need to find.s where an anagram of p is found.s and p will consist only of lowercase English letters ('a' through 'z').p will always be less than or equal to the length of s.Examples
Example 1
Input:
s = "cbaebabacd", p = "abc"
Output:
[0,6]
Note:
The substring starting at index 0 is "cba", which is an anagram of "abc". The substring starting at index 6 is "bac", which is an anagram of "abc".
Example 2
Input:
s = "abab", p = "ab"
Output:
[0,1,2]
Note:
The substring starting at index 0 is "ab", which is an anagram of "ab". The substring starting at index 1 is "ba", which is an anagram of "ab". The substring starting at index 2 is "ab", which is an anagram of "ab".
Example 3
Input:
s = "abacb", p = "abc"
Output:
[1]
Note:
The substring starting at index 1 is "bac", which is an anagram of "abc".
Constraints
1 <= s.length, p.length <= 10^5p.length <= s.lengths and p consist of lowercase English letters only.s.length) and uses minimal additional space.Loading editor...