easy +10 pts

Anagram Dictionary Groups Lite

Group words by their sorted letters to detect anagrams.

Write a function `group_anagrams(words)` that takes a list of lowercase words and returns a list of groups. Each group is a list of words that are anagrams of each other (same letters, same frequency, possibly different order). The order of groups does not matter, but within each group the words must appear in the order they appeared in the input list. Words that are not anagrams of any other word each form a single-word group. The input list may be empty, in which case return an empty list.

Constraints

• 0 ≤ len(words) ≤ 1000 • Each word contains only lowercase English letters. • Each word length is between 1 and 100. • The returned list must contain exactly the groups as described.

Example

>>> group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
>>> group_anagrams(["listen", "silent", "enlist", "hello"])
[["listen", "silent", "enlist"], ["hello"]]
>>> group_anagrams([])
[]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think of a key that is the same for all anagrams of a word.
Sorting the characters of each word gives such a key.
Use a dictionary to accumulate words by their sorted key.
Finally, collect all the groups from the dictionary into a list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.