foolish fly fox's blog

--Stay hungry, stay foolish.
--Forever young, forever weep.



博客主页为:https://foolishflyfox.github.io/CsLearnNote/

49. Group Anagrams

https://leetcode.com/problems/group-anagrams/description/

Description

Given an array of strings, group anagrams together.

Example:

Input:

["eat", "tea", "tan", "ate", "nat", "bat"]

Output:

[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Note:

Solution

class Solution:
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        words = dict()
        for s in strs:
            tp_s = ''.join(sorted(s))
            words.setdefault(tp_s, []).append(s)
        return [s for s in words.values()]