Group Anagrams
Updated
Problem statement
You are given an array of strings words. Two words are anagrams when one can be made by reordering the letters of the other, so they use the same letters the same number of times. An empty string is an anagram of another empty string.
Group the words so that each group holds exactly the words that are anagrams of each other. Return the groups in this order:
- The groups appear in the order in which their first word appears in
words. - Inside a group, the words keep their order from
words. - Repeated words are kept. Every word of
wordsappears exactly once in the output.
Function
groupAnagrams(words: String[]) → List<List<String>>Examples
Example 1
words = ["rail","trip","liar","seat","lair","east","teas"]return = [["rail","liar","lair"],["trip"],["seat","east","teas"]]rail, liar and lair all use the letters a, i, l, r once each, so they form one group. seat, east and teas form another. trip has no partner. The groups appear in the order their first words appear: rail (index 0), trip (index 1), seat (index 3).
Example 2
words = ["no","on","","on",""]return = [["no","on","on"],["",""]]Both copies of on stay in the output, next to no. The two empty strings are anagrams of each other, so they form the second group.
Example 3
words = ["pool","loop","polo","lop","plop"]return = [["pool","loop","polo"],["lop"],["plop"]]pool, loop and polo each have one p, two os and one l. lop has only one o, and plop has two ps and one o. Using the same letters is not enough: the counts must match too, so these two are each on their own.
Constraints
1 <= words.length <= 10000 <= words[i].length <= 20words[i]contains only lowercase English letters.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Group AnagramsMicrosoft · ONSITE INTERVIEW
- Find Duplicate Element PairsGoogle · ONSITE INTERVIEW
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEW
- Group Duplicate Files by ContentPostman · ONSITE INTERVIEW
- Group AnagramsGoldman Sachs · ONSITE INTERVIEW
- Group AnagramsMotive · PHONE SCREEN · ONSITE INTERVIEW
- Group AnagramsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Group AnagramsAkuna Capital · ONSITE INTERVIEW
- Group Anagram StringsLondon Stock Exchange · ONSITE INTERVIEW