难度: 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()