Two words are anagrams if one is a rearrangement of the other's letters, such as "stop" and "pots".
Write groupAnagrams(words) that puts the words into groups. Each group holds all the words that are anagrams of each other. Return an array of groups, where each group is an array of words.
- Order of groups: in the order their first word appears in
words. - Order inside a group: the same order as in
words. - Every word appears in exactly one group. Duplicate words are kept, and they go in the same group.
- Words use only lowercase letters
a–z. A word can be the empty string"", which is only an anagram of another"". - If
wordsis empty, return[]. It can hold up to 30,000 words of up to 10 letters.
Input: words = ["pots", "cat", "stop", "dog", "act", "tops"]
Output: [["pots", "stop", "tops"], ["cat", "act"], ["dog"]]
Input: words = ["a", "b", "a"]
Output: [["a", "a"], ["b"]]
Input: words = []
Output: []