Leetcode: anagrams solution report

Source: Internet
Author: User

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

Note: All inputs will be in lower-case.

Ideas:

Create a hashtable and use the sorted string as the key. Its ansible is used as the arraylist.

The Oracle (N ^ 2) TLE written in brute force mode is used before this question. Use hashtable to write it.
The question is to give a string array to find words consisting of the same letters.
For example:
S = ["ABC", "BCA", "BAC", "BBB", "BBCA", "abcb"]
The answer is:
["ABC", "BCA", "BAC", "BBCA", "abcb"]
Only "BBB" does not contain words of the same letter.

Ref: http://blog.csdn.net/fightforyourdream/article/details/14217985

 1 public class Solution { 2     public List<String> anagrams(String[] strs) { 3         List<String> ret = new ArrayList<String>(); 4          5         if (strs == null) { 6             return ret; 7         } 8          9         HashMap<String, List<String>> map = new HashMap<String, List<String>>();10         11         int len = strs.length;12         for (int i = 0; i < len; i++) {13             String s = strs[i];14             15             // Sort the string.            16             char[] chars = s.toCharArray();17             Arrays.sort(chars);18             String strSort = new String(chars);   19             20             // Create a ArrayList for the sorted string.            21             if (!map.containsKey(strSort)) {22                 map.put(strSort, new ArrayList<String>());23             }24             25             // Add a new string to the list of the hashmap.26             map.get(strSort).add(s);27         }28         29         // go through the map and add all the strings into the result.30         for (Map.Entry<String, List<String>> entry: map.entrySet()) {31             List<String> list = entry.getValue();32             33             // skip the entries which only have one string.34             if (list.size() == 1) {35                 continue;36             }37             38             // add the strings into the list.39             ret.addAll(list);40         }41         42         return ret;43     }44 }
View code

Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/hash/Anagrams.java

Leetcode: anagrams solution report

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.