/
Solution.java
61 lines (56 loc) · 1.74 KB
/
Solution.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
53
54
55
56
57
58
59
60
61
//Given an array of strings, group anagrams together.
//
// Example:
//
//
//Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
//Output:
//[
// ["ate","eat","tea"],
// ["nat","tan"],
// ["bat"]
//]
//
// Note:
//
//
// All inputs will be in lowercase.
// The order of your output does not matter.
//
// Related Topics Hash Table String
// 👍 3604 👎 186
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
// return hashMap(strs);
return sort(strs);
}
private List<List<String>> hashMap(String[] strs) {
if (strs.length == 0) return new ArrayList<>();
HashMap<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] counter = new char[26];
for (char ch : s.toCharArray()) counter[ch - 'a']++;
String keyStr = String.valueOf(counter);
if (!map.containsKey(keyStr)) map.put(keyStr, new ArrayList<>());
map.get(keyStr).add(s);
}
return new ArrayList<>(map.values());
}
private List<List<String>> sort(String[] strs) {
if (strs.length == 0) return new ArrayList<>();
HashMap<String, List<String>> map = new HashMap<>();
for (String s: strs) {
char[] s_char = s.toCharArray();
Arrays.sort(s_char);
String keyStr = String.valueOf(s_char);
if (!map.containsKey(keyStr)) map.put(keyStr, new ArrayList<>());
map.get(keyStr).add(s);
}
return new ArrayList<>(map.values());
}
}
//leetcode submit region end(Prohibit modification and deletion)