Back to DSA
Find All Anagrams in a String
mediumTwo strings are given: a longer text and a shorter pattern. Locate every starting index within the text where a substring of the same length as the pattern is an anagram of the pattern. Return all such starting indices.
Examples
Example 1:
Input:
text = "cbadcba", pattern = "abc"Output:
[0,4]Explanation: At index 0, the substring 'cba' is an anagram of 'abc'. At index 4, the substring 'cba' is again an anagram.
Example 2:
Input:
text = "xyzxyz", pattern = "zy"Output:
[1,4]Explanation: Substrings 'yz' at positions 1 and 4 are both anagrams of 'zy'.
Hints
1234567