public class Solution { public static ArrayListanagrams(String[] strs) { ArrayList res = new ArrayList (); if (strs == null || strs.length == 0) { return res; } HashMap hashmap = new HashMap (); for (int i = 0; i < strs.length; i ++) { String tmp = sortSingleString(strs[i]); if (! hashmap.containsKey(tmp)) { hashmap.put(tmp, i); } else { int loc = hashmap.get(tmp); if (loc != -1) { res.add(strs[loc]); } res.add(strs[i]); hashmap.put(tmp, -1); } } return res; } public static String sortSingleString(String str) { char[] array = str.toCharArray(); Arrays.sort(array); String res = new String(array); return res; } }