-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagramsDay16.py
More file actions
59 lines (48 loc) · 1.51 KB
/
GroupAnagramsDay16.py
File metadata and controls
59 lines (48 loc) · 1.51 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
51
52
53
54
55
56
57
58
59
#Brute Force Approach
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
res = []
visited = [False] * len(strs)
for i in range(len(strs)):
if visited[i]:
continue
group = [strs[i]]
visited[i] = True
for j in range(i + 1, len(strs)):
if sorted(strs[i]) == sorted(strs[j]):
group.append(strs[j])
visited[j] = True
res.append(group)
return res
# Time Complexity:
# Sorting each pair: O(N^2 * K log K) where K is max length of string
#
# N = number of strings
#
# Space Complexity:
# O(N * K) for storing the result
#Better Approach
from collections import defaultdict
from typing import List
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
dic = defaultdict(list)
for word in strs:
key = "".join(sorted(word))
dic[key].append(word)
return list(dic.values())
# TC - O(n log n)
# SC - O(n)
#Optimal Approach
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
dic = defaultdict(list)
for word in strs:
lst = [0]*26 #to store frequency of char from a to z
for char in word:
lst[ord(char)- ord('a')] += 1 #fetching the thing and initializing it 1 if found
lst = tuple(lst)
dic[lst].append(word)
return list(dic.values())
# TC - O(N * K)
# SC - O(N * K)