Given an array of strings, return all groups of strings that is anagrams.
Note:all inputs'll is in lower-case.
Problem-solving ideas: First of all to understand, what is anagrams,ie. "Tea", "tae", "aet", and then very good to do, new a hashmap, using a sequence of string as key, repeated to add elements to the inside, there is a small episode, is the sort of time do not use Priorityqueue, Because Priorityqueue is sorted by heap, only the top element of the heap is guaranteed to be the highest priority (for a long time) the Java implementation is as follows:
static public list<string> anagrams (string[] strs) {list<string> List = new arraylist<string> (); Hashmap<string, list<string>> HM = new hashmap<string, list<string>> (); for (int i = 0; i < STRs . length; i++) {char [] c=strs[i].tochararray (); Arrays.sort (c); String Sortstring=new string (c), if (!hm.containskey (sortstring)) Hm.put (Sortstring.tostring (), New arraylist< String> ()); Hm.get (Sortstring.tostring ()). Add (Strs[i]);} iterator<string> Iterator = Hm.keyset (). Iterator (); while (Iterator.hasnext ()) {String key = Iterator.next (); if ( Hm.get (key). Size () > 1) list.addall (Hm.get (key));} return list;}
Java for Leetcode 049 anagrams