Skip to content

Latest commit

 

History

History
52 lines (39 loc) · 934 Bytes

0049._group_anagrams.md

File metadata and controls

52 lines (39 loc) · 934 Bytes

49. Group Anagrams

难度: Medium

刷题内容

原题连接

内容描述

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]
Note:

All inputs will be in lowercase.
The order of your output does not matter.

解题方案

思路 1

每一个字符串都先排个序看看是不是一样,这样更好判断

class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        mapx = {}
        for i in strs:
            tmp = ''.join(sorted(list(i)))
            if tmp in mapx:
                mapx[tmp].append(i)
            else:
                mapx[tmp] = [i]
        return mapx.values()