Group Anagrams in First-Seen Order
Updated
Problem statement
Two words are anagrams when one can be made by reordering the letters of the other, using every letter exactly as many times as it appears. Split words into groups so that two words share a group exactly when they are anagrams.
Return the groups in a fixed order:
- List the groups in the order in which their first word appears in
words. - Inside a group, keep the words in their input order.
- Keep repeated words: a word that appears several times appears that many times in its group.
Function
groupAnagrams(words: String[]) → List<List<String>>Examples
Example 1
words = ["stop","pots","tops","opts","spot","post","pot"]return = [["stop","pots","tops","opts","spot","post"],["pot"]]The first six words use the letters o, p, s and t once each, so they form one group, in their input order. pot has no s, so it is alone.
Example 2
words = ["listen","enlist","google","silent","elgoog","listen"]return = [["listen","enlist","silent","listen"],["google","elgoog"]]listen starts the first group, which also gets enlist, silent and the second listen. google starts the second group with elgoog. Groups are listed in the order of their first word.
Example 3
words = ["aab","abb","bab","baa"]return = [["aab","baa"],["abb","bab"]]aab and baa have two a's and one b. abb and bab have one a and two b's. Using the same set of letters is not enough: the counts must match.
Constraints
1 <= words.length <= 1000- Each
words[i]has 1 to 10 lowercase English letters.
More Google Onsite Interview questions
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Most Frequent Call Stack Per ThreadRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Build an Inverted Document IndexConfluent · ONSITE INTERVIEWConfluent interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Sort Characters by FrequencySnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Top K Frequent WordsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions