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) { List<String> result=new LinkedList<String>(); Map<String,LinkedList<String>> map=new HashMap<String,LinkedList<String>>(); if(strs==null || strs.length==0) return result; for(int i=0;i<strs.length;i++){ char []arr=strs[i].toCharArray(); Arrays.sort(arr); String s=new String(arr); if(map.containsKey(s)){ map.get(s).add(strs[i]); } else{ LinkedList<String> list=new LinkedList<String>(); list.add(strs[i]); map.put(s,list); } } Set<String> set=map.keySet(); for(String s : set){ if(map.get(s).size()>1){ result.addAll(map.get(s)); } } return result; } }题目没看懂的节奏,看懂了就很简单了。
原文:http://blog.csdn.net/dutsoft/article/details/38562597