public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res = new LinkedList<>();
if (strs == null || strs.length == 0) {
return res;
}
Map<String, List<String>> map = new HashMap<>();
for (String str: strs) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
String temp = new String(chars);
if (map.containsKey(temp)) {
map.get(temp).add(str);
} else {
List<String> list = new LinkedList<>();
list.add(str);
map.put(temp, list);
}
}
for (List<String> list: map.values()) {
res.add(list);
}
return res;
}
}
Group Anagrams
最新推荐文章于 2020-09-09 23:45:30 发布