public class Solution { public static ArrayList<String> anagrams(String[] strs) { ArrayList<String> res = new ArrayList<String>(); if (strs == null || strs.length == 0) { return res; } HashMap<String, Integer> hashmap = new HashMap<String, Integer>(); 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; } }
原文:http://blog.csdn.net/wzy_1988/article/details/19111011