-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathGroupAnagrams.py
More file actions
47 lines (34 loc) · 918 Bytes
/
Copy pathGroupAnagrams.py
File metadata and controls
47 lines (34 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
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.
给一组带有字符串的数组,进行变位词分类。
思路是排序的思路,排序后为同一个的放到一起。
效率就测试来看尚可。
beat 70%
测试地址:
https://leetcode.com/problems/group-anagrams/description/
"""
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
result = {}
for i in strs:
key = ''.join(sorted(i))
try:
result[key].append(i)
except:
result[key] = [i]
return result.values()