Leetcode--group Anagrams--java

Source: Internet
Author: User

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"] ,
Return:

["  ate", "eat", "tea"],  ["Nat", "Tan"],  ["bat"]]

Note:

    1. For the return value, each inner list ' s elements must follow the lexicographic order.
    2. All inputs'll is in lower-case.

Update (2015-08-09):
The signature of the function had been updated to return list<list<string>> instead list<string> of, as suggested here. If you still see your function signature return a list<string> and click the Reload button to reset your code definition .

Subscribe to see which companies asked this question

 Public classSolution { PublicList<list<string>>groupanagrams (string[] strs) {List<list<string>>result =NewArraylist<>(); if(STRs = =NULL|| Strs.length = = 0)             returnresult; //use the dictionary string as key to move its same phrase to a list and store it in HashMapHashmap<string,list<string>>map =NewHashmap<>();  for(inti = 0; i < strs.length; i++) {             Char[] chars =Strs[i].tochararray (); //Dictionary OrderingArrays.sort (chars); String Temp=NewString (chars); if(!Map.containskey (temp)) {List<String> result_list =NewArraylist<>();                 Result_list.add (Strs[i]);             Map.put (temp, result_list); } Else{map.get (temp). Add (Strs[i]); }          }                   //Traverse map to sort the ArrayList in dictionary orderIterator<map.entry<string,list<string>>>iterator =Map.entryset (). iterator ();  while(Iterator.hasnext ()) {Map.entry<String,List<String>> entry =Iterator.next (); List<String> temp_list =Entry.getvalue ();             Collections.sort (temp_list);          Result.add (temp_list); }                 returnresult; }  }  
 Public classSolution { PublicList<list<string>>groupanagrams (string[] strs) {Map<String,ArrayList<String>> map =NewHashmap<string,arraylist<string>>();  for(String str:strs) {string Sortedstr=sortstr (str); if(Map.containskey (sortedstr)) {Map.get (SORTEDSTR). Add (str); }           Else{map.put (sortedstr,NewArraylist<string>());           Map.get (SORTEDSTR). Add (str); }} List<List<String>> result =NewArraylist<list<string>>(Map.values ());  for(list<string>Res:result)       {Collections.sort (res); }       returnresult; }    Privatestring Sortstr (String str) {Char[] Char1 =Str.tochararray ();        Arrays.sort (CHAR1); return NewString (CHAR1); }}

Leetcode--group Anagrams--java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.