Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
<pre name="code" class="java">public class Solution {
public List<String> anagrams(String[] strs) {
Map<String, Integer> map = new HashMap<>();
List<String> res = new ArrayList<String>();
if (strs.length == 0) {
return res;
}
for (int i = 0; i < strs.length; i++) {
char[] c = strs[i].toCharArray();
Arrays.sort(c);
String s = Arrays.toString(c);
if (!map.containsKey(s)) {
map.put(s, 1);
} else {
map.put(s, map.get(s) + 1);
}
}
for (int i = 0; i < strs.length; i++) {
char[] c = strs[i].toCharArray();
Arrays.sort(c);
String s = Arrays.toString(c);
if (map.containsKey(s) && map.get(s) >= 2) {
res.add(strs[i]);
}
}
return res;
}
}原文:http://blog.csdn.net/guorudi/article/details/39643541