-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagrams.java
More file actions
132 lines (99 loc) · 4.54 KB
/
GroupAnagrams.java
File metadata and controls
132 lines (99 loc) · 4.54 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package Algorithms.Hashing;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 23 Sept 2024
* @link 49. Group Anagrams <a href="https://leetcode.com/problems/group-anagrams/">Leetcode link</a>
* @description words with same char frequencies are anagrams
* @topics Array, Hash Table, String, Sorting
* @companies Amazon, Google, Meta, Microsoft, Oracle, Bloomberg, Yandex, Goldman, Zoho, Adobe, J, Apple, Visa, PayPal, IBM, Nvidia, Nutanix, Morgan, TikTok, Uber, Salesforce, ServiceNow, Affirm, EPAM, Walmart, Atlassian, Anduril, Yahoo, Citadel, eBay
*/
public class GroupAnagrams {
public static void main(String[] args) {
String[] strs = new String[]{"eat","tea","tan","ate","nat","bat"};
System.out.println("groupAnagrams => " + groupAnagrams(strs));
System.out.println("groupAnagrams 2 => " + groupAnagrams2(strs));
System.out.println("groupAnagrams 3 => " + groupAnagrams3(strs));
System.out.println("groupAnagrams 4 => " + groupAnagrams4(strs));
System.out.println("groupAnagrams 5 => " + groupAnagrams5(strs));
}
/**
* Approach: Categorize by Sorted char[] String
* Key = String.valueOf(sorted char[]) or new String(sorted char[])
*/
public static List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String str: strs) {
char[] keyValue = str.toCharArray();
Arrays.sort(keyValue);
String key = new String(keyValue);
map.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
}
return new ArrayList<>(map.values()); // or map.values().stream().collect(Collectors.toList());
}
/**
* Key = Arrays.toString(int[])
*/
public static List<List<String>> groupAnagrams2(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for(String str: strs) {
int[] freq = new int[26];
for(int i=0; i<str.length(); i++) {
freq[str.charAt(i)-'a']++;
}
map.computeIfAbsent(Arrays.toString(freq), k -> new ArrayList<>()).add(str);
}
return new ArrayList<>(map.values());
}
/**
* Approach: Categorize by Count
* key = StringBuilder.toString() using int[]
* key = combination of chars in frequencies[26] array as it is already bucket sorted
* key -> we will get the unique key for an anagram as it's already a bucket sort -> no need to sort again
* as we convert int[] to StringBuilder ---> Key construction is heavier
* new int[26]; // bucket sort -> no need to sort the unique chars again
*/
public static List<List<String>> groupAnagrams3(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for(String str: strs) {
int[] freq = new int[26];
for(char c : str.toCharArray()) {
freq[c -'a']++;
}
StringBuilder key = new StringBuilder();
for(int i=0; i<freq.length; i++) {
if(freq[i] > 0) key.append((char)(i+'a')).append(freq[i]);
}
map.computeIfAbsent(key.toString(), k -> new ArrayList<>()).add(str); // or key = Arrays.toString(freq);
}
return new ArrayList<>(map.values());
}
/**
* Key = TreeMap<Character, Integer>.toString()
*/
public static List<List<String>> groupAnagrams4(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for(String str: strs) {
// use TreeMap if key is String because HashMap won't work as we need key order, Eg: ["bur", "rub"]
Map<Character, Integer> freq = new TreeMap<>();
for(int i=0; i<str.length(); i++) {
freq.merge(str.charAt(i), 1, Integer::sum);
}
map.computeIfAbsent(freq.toString(), k -> new ArrayList<>()).add(str);
}
return new ArrayList<>(map.values());
}
/**
* Key = HashMap<Character, Integer>
*/
public static List<List<String>> groupAnagrams5(String[] strs) {
Map<Map<Character, Integer>, List<String>> map = new HashMap<>();
for(String str: strs) {
Map<Character, Integer> freq = str.chars().mapToObj(i->(char)i).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)));
map.computeIfAbsent(freq, k->new ArrayList<String>()).add(str);
}
return new ArrayList<>(map.values());
}
}