Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
public class Solution {
public List<String> anagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (String s : strs) {
char[] arr = s.toCharArray();
Arrays.sort(arr);
String t = new String(arr);
if (!map.containsKey(t)) {
map.put(t, new ArrayList<String>());
}
map.get(t).add(s);
}
List<String> res = new ArrayList<String>();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
if (entry.getValue().size() > 1) {
res.addAll(entry.getValue());
}
}
return res;
}
}
1272

被折叠的 条评论
为什么被折叠?



