You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
53 lines
1.3 KiB
2 years ago
|
package class28;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Arrays;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class Problem_0049_GroupAnagrams {
|
||
|
|
||
|
public static List<List<String>> groupAnagrams1(String[] strs) {
|
||
|
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
|
||
|
for (String str : strs) {
|
||
|
int[] record = new int[26];
|
||
|
for (char cha : str.toCharArray()) {
|
||
|
record[cha - 'a']++;
|
||
|
}
|
||
|
StringBuilder builder = new StringBuilder();
|
||
|
for (int value : record) {
|
||
|
builder.append(String.valueOf(value)).append("_");
|
||
|
}
|
||
|
String key = builder.toString();
|
||
|
if (!map.containsKey(key)) {
|
||
|
map.put(key, new ArrayList<String>());
|
||
|
}
|
||
|
map.get(key).add(str);
|
||
|
}
|
||
|
List<List<String>> res = new ArrayList<List<String>>();
|
||
|
for (List<String> list : map.values()) {
|
||
|
res.add(list);
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
public static List<List<String>> groupAnagrams2(String[] strs) {
|
||
|
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
|
||
|
for (String str : strs) {
|
||
|
char[] chs = str.toCharArray();
|
||
|
Arrays.sort(chs);
|
||
|
String key = String.valueOf(chs);
|
||
|
if (!map.containsKey(key)) {
|
||
|
map.put(key, new ArrayList<String>());
|
||
|
}
|
||
|
map.get(key).add(str);
|
||
|
}
|
||
|
List<List<String>> res = new ArrayList<List<String>>();
|
||
|
for (List<String> list : map.values()) {
|
||
|
res.add(list);
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
}
|