Repository navigation
Expand file tree
/
Copy path49_Group_Anagrams.py
More file actions
50 lines (46 loc) · 1.6 KB
/
Copy path49_Group_Anagrams.py
File metadata and controls
50 lines (46 loc) · 1.6 KB
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
# My initial solution. 100 / 101 test cases passed. Got wrong answer on the last test case: https://tinyurl.com/wfbeu8w
from collections import Counter
from collections import defaultdict
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
seenMap = defaultdict(bool)
resultMap = defaultdict(list)
counterArr = []
for currStr in strs:
counter = Counter(currStr)
counterArr.append(counter)
for i in range(len(strs)):
str1 = strs[i]
counter1 = counterArr[i]
if not seenMap[str1]:
resultMap[str1].append(str1)
seenMap[str1] = True
else:
continue
for j in range(i + 1, len(counterArr)):
str2 = strs[j]
counter2 = counterArr[j]
if not seenMap[str2]:
if counter1 == counter2:
resultMap[str1].append(str2)
seenMap[str2] = True
elif str1 == str2:
resultMap[str2].append(str2)
return resultMap.values()
from collections import Counter
from collections import defaultdict
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
resultMap = defaultdict(list)
for s in strs:
# resultMap[tuple(sorted(s))].append(s)
resultMap[''.join(sorted(s))].append(s)
return resultMap.values()