Group Anagrams
Updated
Problem statement
Given an array of strings words, group words that are anagrams. Two words are anagrams when one can be rearranged into the other, using every character exactly once.
Return the groups in the order that their first member appears in words. Preserve input order within each group. Keep duplicate words as separate entries. Empty strings are valid and belong to the same group.
Function
groupAnagrams(words: String[]) → List<List<String>>Examples
Example 1
words = ["eat","tea","tan","ate","nat","bat"]return = [["eat","tea","ate"],["tan","nat"],["bat"]]The first anagram families appear with eat, tan, then bat; words stay in their original order within each family.
Example 2
words = ["","a","","a"]return = [["",""],["a","a"]]Both empty strings share the first group. Both copies of a remain in the second group.
Constraints
0 <= words.length <= 10^4.0 <= words[i].length <= 100.- The total number of characters is at most
2 × 10^5. - Every word contains only lowercase English letters from
athroughz.
More Vanguard Online Assessment questions
- Two SumOA · Seen Sep 2026
- Valid ParenthesesOA · Seen Sep 2026
- Are They PangramsOA · Seen Nov 2025
- Missing WordsOA · Seen Nov 2025
- Spam DetectionOA · Seen Nov 2025
- Binge WatchingOA · Seen Jan 2023
- Minimum Moves Around a Circular ArrayOA · Seen Aug 2022
- Maximum Occurring CharacterOA · Seen Mar 2022
- Picking TicketsOA · Seen Mar 2022
- Tool List: Two Smallest ValuesOA · Seen Nov 2021
Similar questions at other companies
- Password Clues and the Next CommandOpenAI · OAOpenAI interview questions
- Decode Words with Anagram KeysCresta · OACresta interview questions
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Maximum Valid Substring FrequencyMicrosoft · OAMicrosoft interview questions
- Perfect SubstringAXQ Capital · OA
- Longest Distinct StringSuperhuman · OASuperhuman interview questions
- Longest String ChainVisa · OAVisa interview questions
- Longest Substring Without Repeating CharactersAkuna Capital · OAAkuna Capital interview questions
- How Many Sentences?Chubb · OAChubb interview questions