/
049. Group Anagrams.py
67 lines (57 loc) · 1.58 KB
/
049. Group Anagrams.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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.
# Solution 1 sorted str
class Solution(object):
def groupAnagrams(self, strs):
"""
O(nklgk)
k-length of word in list
:type strs: List[str]
:rtype: List[List[str]]
"""
dic = {}
for word in strs:
ori = word
# imp
word = str(sorted(word))
if word not in dic:
dic[word] = [ori]
else:
dic[word].append(ori)
return dic.values()
# Note: All inputs will be in lower-case.
# Solution 2
# we can create size equals to 26 list to store
class Solution(object):
def groupAnagrams(self, strs):
"""
O(nk)
:type strs: List[str]
:rtype: List[List[str]]
"""
if not strs:
return []
dic = {}
for word in strs:
# init count
count = [0 for _ in range(26)]
for char in word:
count[ord(char) - ord('a')] += 1
# rebuild str
temp = ""
for i in range(26):
if count[i] != 0:
temp += chr(i+97) * count[i]
#print temp
if temp not in dic:
dic[temp] = [word]
else:
dic[temp].append(word)
return dic.values()