-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagramsLcci.java
More file actions
64 lines (54 loc) · 1.81 KB
/
GroupAnagramsLcci.java
File metadata and controls
64 lines (54 loc) · 1.81 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
package leetcode;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* GroupAnagramsLcci
* https://leetcode-cn.com/problems/group-anagrams-lcci/
* 面试题 10.02. 变位词组
* https://leetcode-cn.com/problems/group-anagrams-lcci/solution/jian-li-zi-fu-chuan-suo-yin-by-oshdyr-9wbi/
*
* @author tobin
* @since 2021-07-18
*/
public class GroupAnagramsLcci {
public static void main(String[] args) {
GroupAnagramsLcci sol = new GroupAnagramsLcci();
List<List<String>> res = sol.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
for (List<String> list : res) {
for (String str : list) {
System.out.print(str + ", ");
}
System.out.println();
}
}
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
int[] indexArr = new int[26];
for (int i = 0; i < str.length(); i++) {
int idx = str.charAt(i) - 'a';
indexArr[idx]++;
}
StringBuilder indexSb = new StringBuilder();
for (int unit : indexArr) {
indexSb.append(unit);
}
String index = indexSb.toString();
List<String> history;
if (map.containsKey(index)) {
history = map.get(index);
} else {
history = new LinkedList<>();
}
history.add(str);
map.put(index, history);
}
List<List<String>> result = new LinkedList<>();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
result.add(entry.getValue());
}
return result;
}
}