-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.java
More file actions
32 lines (26 loc) · 1.06 KB
/
Copy pathDay10.java
File metadata and controls
32 lines (26 loc) · 1.06 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
import java.util.*;
public class GroupAnagrams {
public static List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) return new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] arr = s.toCharArray();
Arrays.sort(arr);
String key = new String(arr);
map.computeIfAbsent(key, k -> new ArrayList<>()).add(s);
}
return new ArrayList<>(map.values());
}
public static void main(String[] args) {
String[] test1 = {"eat", "tea", "tan", "ate", "nat", "bat"};
System.out.println(groupAnagrams(test1));
String[] test2 = {""};
System.out.println(groupAnagrams(test2));
String[] test3 = {"a"};
System.out.println(groupAnagrams(test3));
String[] test4 = {"abc", "bca", "cab", "xyz", "zyx", "yxz"};
System.out.println(groupAnagrams(test4));
String[] test5 = {"abc", "def", "ghi"};
System.out.println(groupAnagrams(test5));
}
}