Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. The output should be in sorted order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word 'anagram' can be rearranged into 'nagaram'.
[ "cbaebabacd", "abc" ]
Explanation. The substring with start index = 0 is 'cba', which is an anagram of 'abc'. The substring with start index = 6 is 'bac', which is also an anagram of 'abc'.
[ "abab", "ab" ]
Explanation. The substring with start index = 0 is 'ab', which is an anagram of 'ab'. The substring with start index = 1 is 'ba', which is an anagram of 'ab'. The substring with start index = 2 is 'ab', which is an anagram of 'ab' again.
Follow-up: How will your solution perform with larger input sizes, and can it be optimized further?
- **Length of both strings `s` and `p` will be at most 20100.** - **The strings consist of lowercase English letters only.**
- Views
- 3