leetcode49: group anagrams

Link

Given an array of strings, group anagrams together.

For example:

Given:

"tea", "tan", "ate", "nat", "bat"]```,
1
2
Return:

[
[“ate”, “eat”,”tea”],
[“nat”,”tan”],
[“bat”]
]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 2. Analysis
# 3. Solution(s)
```python
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
dic = dict()
for i in strs:
try:
dic[str(sorted([x for x in i]))].append(i) # Set ans List are unhashable
except:
dic[str(sorted([x for x in i]))] = [i]
res = list()
for k,v in dic.iteritems():
res.append(v)
return res