-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathproblem14.py
More file actions
31 lines (22 loc) · 779 Bytes
/
problem14.py
File metadata and controls
31 lines (22 loc) · 779 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
31
from collections import defaultdict
from typing import List
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
if not strs:
return None
hashMap = defaultdict(list)
res = []
for str in strs:
#26 length array initialize
key = [0]*26
for char in str:
key[ord(char)-ord('a')] += 1
hashMap[tuple(key)].append(str)
for key,values in hashMap.items():
res.append(values)
return res
# Time Complexity: O(n*k + n) = O(N*k)
# Space Complexity: O(nK + n + nk) = O(n*k) worse case: hashMap has n keys of O(1) and values it stores all
# the strings (nk)
# n = number of strings
# k = average length of each string