-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP0049_GroupAnagrams.java
52 lines (47 loc) · 1.84 KB
/
P0049_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
47
48
49
50
51
52
package yyl.leetcode.p00;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <h3>字母异位词分组</h3><br>
* 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。<br>
* 示例:<br>
* 输入: ["eat", "tea", "tan", "ate", "nat", "bat"],<br>
* 输出: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ]<br>
* 说明: 所有输入均为小写字母。 不考虑答案输出的顺序。<br>
*/
public class P0049_GroupAnagrams {
public static void main(String[] args) {
Solution solution = new Solution();
String[] sample = {"eat", "tea", "tan", "ate", "nat", "bat"};
System.out.println(solution.groupAnagrams(sample));
}
// 时间复杂度:O(nK), K是字符串的最长长度
// 空间复杂度:O(nK), 存储在MAP中的全部信息内容
static class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String str : strs) {
String key = digest(str);
List<String> list = map.get(key);
if (list == null) {
map.put(key, list = new ArrayList<>());
}
list.add(str);
}
return new ArrayList<>(map.values());
}
private String digest(String str) {
int[] alpha = new int[26];
for (int i = 0; i < str.length(); i++) {
alpha[str.charAt(i) - 'a']++;
}
StringBuilder buffer = new StringBuilder();
for (int a : alpha) {
buffer.append(a).append(',');
}
return buffer.toString();
}
}
}