FastPrepAll Anagram Start Indices
Problems/All Anagram Start Indices

All Anagram Start Indices

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon SDE2 interview list with acbadabcaa and aabc producing indices 0, 5, and 6. Problem match confidence: approximately 98%.

Given lowercase strings s and p, return every starting index where a substring of s is an anagram of p.

Return indices in increasing order. Overlapping matches are included.

Function

findAnagrams(s: String, p: String) → List<Integer>

Examples

Example 1

s = "acbadabcaa"p = "aabc"return = [0,5,6]

The length-four substrings at 0, 5, and 6 have exactly the pattern frequencies.

Example 2

s = "cbaebabacd"p = "abc"return = [0,6]

cba and bac are anagrams of abc.

Example 3

s = "abab"p = "ab"return = [0,1,2]

All three length-two windows match, including overlaps.

Constraints

  • 1 ≤ s.length, p.length ≤ 100000.
  • s and p contain only lowercase English letters.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        // write your code here
    }
}
Checking account…