-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString49.java
More file actions
37 lines (34 loc) · 950 Bytes
/
String49.java
File metadata and controls
37 lines (34 loc) · 950 Bytes
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
package string;
import java.util.*;
/**
* @ProjectName: leetcode
* @Package: string
* @ClassName: String49
* @Author: markey
* @Description:
* @Date: 2020/5/17 22:56
* @Version: 1.0
*/
public class String49 {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String s: strs) {
String sortedS = sort(s);
if (map.containsKey(sortedS)) {
map.get(sortedS).add(s);
} else {
List<String> list = new ArrayList<>();
list.add(s);
map.put(sortedS, list);
}
}
List<List<String>> res = new ArrayList<>();
map.keySet().forEach(key -> res.add(map.get(key)));
return res;
}
private String sort(String s) {
char[] chars = s.toCharArray();
Arrays.sort(chars);
return Arrays.toString(chars);
}
}