Skip to contentKEEP THE CRAFTTHE SANCTUARY

Group Anagrams

Put words that are anagrams of each other into the same group.

The problem

Implement Solution.groupAnagrams(words). Two words are anagrams when one can be made by rearranging the letters of the other, using every letter exactly as many times. Return the words split into groups so that two words are in the same group exactly when they are anagrams. Keep every word, including repeated words and empty strings. The order of the groups, and of the words inside a group, does not matter.

Starter code

class Solution:
    def groupAnagrams(self, words: list[str]) -> list[list[str]]:
        pass

Public examples

  1. words = ["eat", "tea", "tan", "ate", "nat", "bat"]gives[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
  2. words = ["", "", "a"]gives[["", ""], ["a"]]
  3. words = ["abc", "abd"]gives[["abc"], ["abd"]]
Open it in the practice book
One thought at a time.