"""
def anagrams(self, strs):
dic = {}
for s in strs:
dic[tuple(sorted(s))] = dic.get(tuple(sorted(s)),[]) + [s]
res = []
[res.extend(item) for item in dic.values() if len(item) > 1]
return res
"""
# Here is an updated version, where the order in each inner group is maintained:
def groupAnagrams(self, strs):
dic = {}
for s in strs:
dic[tuple(sorted(s))] = dic.get(tuple(sorted(s)),[]) + [s]
return [sorted(item) for item in dic.values()]
Python easy to understand solution with dictionary.


Yeah,this can pass.My previous solution is similar to yours,but it always failed due to time limit exceed.Thanks to yours,i understand why that happend.Thanks