LeetCode -- Anagrams

Source: Internet
Author: User

LeetCode -- Anagrams

 

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

 

Anman is the English word used in the game of misplacement and word formation, this word comes from the Greek roots ana-And grahpein, which have the meaning of "reverse" or "re. The inverted word formation is a kind of word games (more accurately, it is a kind of "word games"). It re-arranges the letters in a word or short sentence, each occurrence of all letters in the original text is used to create other words or short sentences. Http://zh.wikipedia.org/wiki/%E6%98%93%E4%BD%8D%E6%9E%84%E8%AF%8D%E6%B8%B8%E6%88%8F

 

public List
 
   anagrams(String[] strs) {List
  
    list = new ArrayList
   
    ();Map
    
     > map = new HashMap
     
      >();for(String str : strs){char[] ch = str.toCharArray();Arrays.sort(ch);String s = new String(ch);if(map.containsKey(s))map.get(s).add(str);else{List
      
        li = new ArrayList
       
        ();li.add(str);map.put(s,li);}}for(List
        
          ls : map.values()){if(ls.size() > 1)list.addAll(ls);}return list;}
        
       
      
     
    
   
  
 

 

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.