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
My code:
class Solution:
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
result=collections.defaultdict(list)
for item in strs:
tmp_list=[]
for i in range(len(item)):
tmp_list.append(item[i])
tmp_list.sort()
reform_str=str(tmp_list)
result[reform_str].append(item)
record=[]
for item in result.keys():
record.append(result[item])
return record