-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGroupAnagrams.java
46 lines (44 loc) · 1.4 KB
/
GroupAnagrams.java
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
package io.ziheng.hashtable.leetcode;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.stream.Collectors;
/**
* LeetCode 49. Group Anagrams
* https://leetcode.com/problems/group-anagrams/
*/
public class GroupAnagrams {
public static void main(String[] args) {
String[] strs = new String[]{
"eat","tea","tan","ate","nat","bat",
};
System.out.println(
new GroupAnagrams().groupAnagrams(strs)
);
}
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0) {
return new LinkedList<>();
}
return groupAnagramsHashWay(strs);
}
private List<List<String>> groupAnagramsHashWay(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] sCharArry = s.toCharArray();
Arrays.sort(sCharArry);
String orderedKey = String.valueOf(sCharArry);
if (!map.containsKey(orderedKey)) {
List<String> resultList = new LinkedList<>();
resultList.add(s);
map.put(orderedKey, resultList);
} else {
map.get(orderedKey).add(s);
}
}
return map.values().stream().collect(Collectors.toList());
}
}
/* EOF */