Find All Anagrams in a String
Return the start index of every contiguous piece of s that is an anagram of p (it has the same letters as p in any order). Give the indices in increasing order. Overlapping pieces all count, and if none exist return an empty list.
Example 1
s "cbaebabacd"
p "abc"
returns [0,6]
Pieces cba at 0 and bac at 6.
Example 2
s "abab"
p "ab"
returns [0,1,2]
Starts 0, 1 and 2 all work.
Constraints
1 <= len(s), len(p) <= 100- Lowercase letters only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Two pointers problems
- Fruit Into BasketsMedium
- Longest Complete Vowel StretchMedium
- Longest Ones After Deleting OneMedium
- Longest Palindromic SubstringMedium
- Longest Repeating Character ReplacementMedium
- Longest Stretch With No Letter ThriceMedium
- Longest Substring Without Repeating CharactersMedium
- Max Consecutive Ones With FlipsMedium