Find the First Anagram Start Index
Updated
Problem statement
Given strings s and p, return the smallest zero-based starting index of a substring of s that is an anagram of p. Return -1 if no such substring exists.
An anagram has exactly the same character counts as p, so each candidate window has length p.length. Overlapping windows are valid candidates, and repeated letters must match with the same multiplicities. If several windows match, return only the first start.
Function
findAnagramStart(s: String, p: String) → intExamples
Example 1
s = "cbaebabacd"p = "abc"return = 0The first length-three anagram window is cba, starting at index zero.
Example 2
s = "zzbacxxabc"p = "abc"return = 2The first matching window is bac at index two; a later match does not change the answer.
Example 3
s = "a"p = "ab"return = -1The pattern is longer than the text, so no complete window exists.
Constraints
1 <= s.length, p.length <= 10^4.- Both strings contain only lowercase English letters.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Longest Substring with At Most K Distinct CharactersAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Longest Substring Without Repeating CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Longest Substring Without Repeating CharactersAngel One · PHONE SCREEN
- Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Longest Substring Without Repeating CharactersNykaa · PHONE SCREENNykaa interview questions
- Longest Substring Without Repeating CharactersIBM · PHONE SCREENIBM interview questions
- Longest Substring Without Repeating CharactersTikTok · PHONE SCREENTikTok interview questions
- Find Word-Multiset WindowsAmazon · PHONE SCREEN
- Longest Substring Without Repeating CharactersGoogle · ONSITE INTERVIEWGoogle interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences