-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGroupAnagrams.java
More file actions
72 lines (59 loc) · 2.37 KB
/
Copy pathGroupAnagrams.java
File metadata and controls
72 lines (59 loc) · 2.37 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
// Given an array of strings, group anagrams together.
// See: https://leetcode.com/problems/group-anagrams/
package leetcode.array_and_hashtable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class GroupAnagrams {
/**
* Efficient solution with counting sort instead Arrays.sort(something).
*/
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
StringBuilder sb = new StringBuilder();
for (String currStr : strs) {
int[] counts = new int[26];
for (int i = 0; i < currStr.length(); i++)
counts[currStr.charAt(i) - 97]++;
sb.setLength(0);
for (int i = 0; i < counts.length; i++)
for (int j = 0; j < counts[i]; j++)
sb.append('a' + i);
map.computeIfAbsent(sb.toString(), k -> new LinkedList<>()).add(currStr);
}
return new LinkedList<>(map.values());
}
/**
* This solution is actually slower than the initial solution.
*/
public List<List<String>> groupAnagrams_var2(String[] strs) {
Map<Map<Character, Integer>, List<String>> map = new HashMap<>();
for (String currStr : strs) {
Map<Character, Integer> currMap = new HashMap<>();
for (int i = 0; i < currStr.length(); i++)
currMap.put(currStr.charAt(i), currMap.getOrDefault(currStr.charAt(i), 0) + 1);
map.computeIfAbsent(currMap, k -> new LinkedList<>()).add(currStr);
}
return new LinkedList<>(map.values());
}
/**
* Initial solution.
*/
public List<List<String>> groupAnagrams_var1(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String currStr : strs) {
char[] curr = currStr.toCharArray();
Arrays.sort(curr);
map.computeIfAbsent(new String(curr), k -> new LinkedList<>()).add(currStr);
}
return new LinkedList<>(map.values());
}
public static void main(String[] args) {
GroupAnagrams sln = new GroupAnagrams();
System.out.println(
sln.groupAnagrams(new String[] { "eat", "tea", "tan", "ate", "nat", "bat" }));
System.out.println(sln.groupAnagrams(new String[] { "", "" }));
}
}