forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagrams.java
More file actions
30 lines (22 loc) · 672 Bytes
/
GroupAnagrams.java
File metadata and controls
30 lines (22 loc) · 672 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
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class GroupAnagrams {
// 12ms
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] cc = s.toCharArray();
Arrays.sort(cc);
String t = new String(cc);
List<String> list = map.get(t);
if (list == null) {
list = new LinkedList<>();
map.put(t, list);
}
list.add(s);
}
return new LinkedList<>(map.values());
}
}