Group anagrams together
Coding problemMediumAsked in: Amazon, Goldman Sachs, Google, Meta, Microsoft, Morgan Stanley
Given a list of words, put words that are anagrams of each other (the same letters, the same number of times, in any order) into the same group. Return the groups.
The groups can be in any order, and the words inside a group can be in any order. A word that appears twice appears twice in its group. An empty word is a valid word.
Examples
Example 1
Input: words = ["eat","tea","tan","ate","nat","bat"]
Output: [["eat","tea","ate"],["tan","nat"],["bat"]]
Example 2
Input: words = [""]
Output: [[""]]
Example 3
Input: words = ["a","b","a"]
Output: [["a","a"],["b"]]
Constraints
- The list holds between 0 and 10,000 words
- Each word has between 0 and 100 lower-case English letters
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.