-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.py
36 lines (32 loc) · 943 Bytes
/
day6.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
# def groupAnagrams(strs):
# """
# :type strs: List[str]
# :rtype: List[List[str]]
# """
# newstrarr = []
# for i in range(1,len(strs)):
# for j in range(len(strs[i])):
# if strs[i][j:] and strs[i][0:j] in strs[i-1]==True:
# newstrarr.append(strs[i])
# GROUP ANAGRAMS
# Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
# Output:
# [
# ["ate","eat","tea"],
# ["nat","tan"],
# ["bat"]
# ]
def groupAnagrams(strs):
ans = collections.defaultdict(list)
for s in strs:
ans[tuple(sorted(s))].append(s)
return ans.values()
# def groupAnagrams(strs):
# ans = collections.defaultdict(list)
# for s in strs:
# count = [0] * 26
# for c in s:
# count[ord(c) - ord('a')] += 1
# ans[tuple(count)].append(s)
# return ans.values()
# groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])