Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Return:
[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]
Note: All inputs will be in lower-case.
Subscribe to see which companies asked this question
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
word_hash = {}
result = []
for string in strs:
t = tuple(sorted(list(string)))
if t in word_hash:
word_hash[t].append(string)
else:
word_hash[t] = [string]
for key in word_hash:
result.append(word_hash[key])
return result