Self-explanatory Python solution


  • 0
    S

    Really clean and simple. Just uses the sorted word as the key.

        def groupAnagrams(self, strs):
            anagrams = {}
            for word in strs:
                sorted_word = "".join(sorted(word))
                if sorted_word not in anagrams:
                    anagrams[sorted_word] = [word]
                else:
                    anagram_list = anagrams[sorted_word]
                    anagram_list.append(word)
                    anagrams[sorted_word] = anagram_list
            return anagrams.values()
    

  • 0
    M

    @scottfits said in Self-explanatory Python solution:

    anagrams.values()

    anagram_list = anagrams[sorted_word]
    anagram_list.append(word)
    anagrams[sorted_word] = anagram_list

    can be write as follow
    anagrams[sorted_word] .append(word)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.