public class Solution {
public String hashcoding(String s){
int count[]=new int[26];
StringBuffer sb=new StringBuffer("H");
for(int i=0;i<s.length();i++)
count[s.charAt(i)-'a']++;
for(int i=0;i<26;i++)
if(count[i]>0)sb.append(""+(i+(int)'a')+count[i]);
return sb.toString();
}
public ArrayList<String> anagrams(String[] strs) {
HashMap<String,ArrayList<String>> hm= new HashMap<String,ArrayList<String>>();
ArrayList<String> rl=new ArrayList<String>();
int ecount=0;
for(int i=0;i<strs.length;i++)
{
String hc=hashcoding(strs[i]);
ArrayList<String> al=hm.get(hc);
if(al==null)al=new ArrayList<String>();
al.add(new String(strs[i]));
if(hm.get(hc)==null)hm.put(hc,al);
}
for(ArrayList<String> al:hm.values())
{
if(al.size()>1)rl.addAll(al);
}
return rl;
}
}
No comments:
Post a Comment