/* Author: King, wangjingui@outlook.com Date: Dec 25, 2014 Problem: Anagrams Difficulty: Easy Source: https://oj.leetcode.com/problems/anagrams/ Notes: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Solution: Sort the string to see if they're anagrams. */ import java.util.Map.Entry; public class Solution { public List anagrams(String[] strs) { ArrayList res = new ArrayList(); HashMap> group = new HashMap>(); if (strs.length == 0) return res; for (int i = 0; i < strs.length; ++i) { char[] tmp = strs[i].toCharArray(); Arrays.sort(tmp); String s = String.valueOf(tmp); if(group.containsKey(s)) (group.get(s)).add(strs[i]); else { ArrayList t = new ArrayList(); t.add(strs[i]); group.put(s,t); } } Iterator>> iter = group.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); ArrayList val = (ArrayList) entry.getValue(); if (val.size() > 1) res.addAll(val); } return res; } }