Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
We can sort each string and store it in a hashmap to identify when a string has an anagram. The algorithm runs inO(n k log k).
Code from http://www.darrensunny.me/leetcode-anagrams/public ArrayList<String> anagrams(String[] strs) {
ArrayList<String> res = new ArrayList<String>();
if(strs == null || strs.length == 0)
return res;
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for(int i=0;i<strs.length;i++)
{
char[] charArr = strs[i].toCharArray();
Arrays.sort(charArr);
String str = new String(charArr);
if(map.containsKey(str))
{
map.get(str).add(strs[i]);
}
else
{
ArrayList<String> list = new ArrayList<String>();
list.add(strs[i]);
map.put(str,list);
}
}
Iterator iter = map.values().iterator();
while(iter.hasNext())
{
ArrayList<String> item = (ArrayList<String>)iter.next();
if(item.size()>1)
res.addAll(item);
}
return res;
}
Read full article from My Own Notes: Leetcode: Anagrams
No comments:
Post a Comment