Find All Anagram Starting Positions
Updated
Problem statement
Given strings s and p, find every starting position in s where a contiguous substring is an anagram of p. An anagram contains exactly the same letters with the same counts, possibly in a different order.
Return zero-based starting indices in ascending order. Matches may overlap. If p is longer than s or no match exists, return an empty array.
Function
findAnagrams(s: String, p: String) → List<Integer>Examples
Example 1
s = "cbaebabacd"p = "abc"return = [0,6]The length-three substrings starting at 0 and 6 are cba and bac. Each contains one a, one b, and one c. No other length-three substring has those counts.
Example 2
s = "abab"p = "ab"return = [0,1,2]The three length-two substrings are ab, ba, and ab. They all match, including the overlapping match starting at 1.
Constraints
1 <= s.length <= 30000.1 <= p.length <= 30000.- Both strings contain only lowercase English letters a through z.
More Intuit Online Assessment questions
- Employee Promotion OpportunitiesOA · Seen Oct 2026
- Next Pascal Row CoefficientOA · Seen Oct 2026
- Parse Router LogsOA · Seen Oct 2026
- Longest Repeated Letter Block with Adjacent SwapsOA · Seen Oct 2026
- Maximum Square-Free DivisorOA · Seen Oct 2026
- Two Tribes from Taller-Person CountsOA · Seen Oct 2026
- Shortest Path from an Encoded GraphOA · Seen Sep 2026
- Oldest and Youngest EmployeesOA · Seen Sep 2026
- Pressure-Isolation Shutdown OrderOA · Seen Sep 2026
- Submask Sum QueriesOA · Seen Sep 2026
Similar questions at other companies
- Maximum Valid Substring FrequencyMicrosoft · OAMicrosoft interview questions
- Perfect SubstringAXQ Capital · OA
- Longest Distinct StringSuperhuman · OASuperhuman interview questions
- Longest Substring Without Repeating CharactersAkuna Capital · OAAkuna Capital interview questions
- Perfect SubstringsVisa · OAVisa interview questions
- Vowel SubstringSnowflake · OASnowflake interview questions
- Count Distinct Fixed-Length SubstringsAdobe · OAAdobe interview questions
- Video ID Generation (Generic)TikTok · OATikTok interview questions
- Unaligned DedupeRubrik · OARubrik interview questions
- Get Max OccurrencesIBM · OAIBM interview questions