-
Notifications
You must be signed in to change notification settings - Fork 0
/
049字母异位词分组.py
35 lines (32 loc) · 1 KB
/
049字母异位词分组.py
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
# -*- coding:utf-8 -*-
__author__ = 'ShawDa'
class Solution:
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
res_dict = {}
for str in strs:
tuple_str = tuple(sorted(str))
if tuple_str not in res_dict:
res_dict[tuple_str] = [str]
else:
res_dict[tuple_str].append(str)
return list(res_dict.values())
def groupAnagrams1(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
res_dict = {}
for str in strs: # 因为都是小写字母,所以可以对字母计数
count = [0] * 26
for c in str:
count[ord(c)-ord('a')] += 1
tuple_count = tuple(count)
if tuple_count not in res_dict:
res_dict[tuple_count] = [str]
else:
res_dict[tuple_count].append(str)
return list(res_dict.values())