-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup_anagrams.py
More file actions
30 lines (23 loc) · 983 Bytes
/
group_anagrams.py
File metadata and controls
30 lines (23 loc) · 983 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
# Given an array of strings strs, group all anagrams together into sublists. You may return the output in any order.
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
# create a hashmap where each key maps to a list
# key is the count of individual chars in the string
# {
# [0, 0, 0, 1, 0, 2, 0, ...]: []
# }
res = defaultdict(list)
for s in strs:
# create a list of 26 0's
count = [0] * 26
for c in s:
# total the number of each individual char in a string
# using the ascii value eg. ascii "a" = 80 ascii b = "81" 81-80 = index 1
count[ord(c) - ord("a")] += 1
# for that count array append the string
res[tuple(count)].append(s)
# return hash.values()
return res.values()
# m = len input strings
# n = len of string
# O(m*n*26)