Leetcode: anagrams

Source: Internet
Author: User
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.

Analysis: the simple version of this question is to determine whether two words are ansible. Generally, there are two methods. The first method is to use hashmap, where the key is a character, and the value is the number of occurrences. If the two words make up the same hashmap, It is anagram. In implementation, a hashmap is constructed and removed one by one from the previous hashmap. If the hashmap is empty, true is returned. The time complexity of this method is O (m + n), and m and n are the length of two words respectively. The space complexity is O (character set size ). The second method is to sort two words. If the results are the same after sorting, the two words are ansible. The time complexity of this method depends on the sorting algorithm. Generally, the Sorting Algorithm is O (nlogn). If the character set is small enough, you can also use a linear sorting algorithm. However, in general, the first method is simpler to judge two words.
Next, let's take a look at this question. In many strings, it is classified by anchor. If hashmap is used and then matched by two, it will be troublesome to group. However, if the sort method is used, a major advantage is that the sorted string can be used as a key, that is, the ID of a class, so as long as each string is sorted, then create a hashmap. The key is the sorted string, and the value is all the strings belonging to the key class. This makes it easier to classify. Assume that we have n strings and the maximum length of the string is K. the time complexity of this algorithm is O (nklogk), where O (klogk) is to sort each string (can be improved if linear algorithms are used ). The space complexity is O (NK), that is, the size of hashmap. The implementation code is as follows:

Notice: this error occurs:

Input: ["", ""] Output: [] expected: ["", ""]

The reason is that there are 8th rows. I previously wrote: String afterprocess = temp. tostring (); it seems that "" is not used. Change it to string afterprocess = new string (temp); That's right. Why?

There are also 19th rows of practice. The first time I use 21st rows, I need to know

 1 public class Solution { 2     public ArrayList<String> anagrams(String[] strs) { 3         HashMap<String, ArrayList<String>> anagramlist = new HashMap<String, ArrayList<String>>(); 4         ArrayList<String> results = new ArrayList<String>(); 5         if (strs == null || strs.length ==0) return results; 6         for (String single : strs) { 7             char[] temp = single.toCharArray(); 8             java.util.Arrays.sort(temp); 9             String afterprocess = new String(temp);10             if (anagramlist.containsKey(afterprocess)) {11                 anagramlist.get(afterprocess).add(single);12             }13             else {14                 ArrayList<String> list = new ArrayList<String>();15                 list.add(single);16                 anagramlist.put(afterprocess, list);17             }18         }19         for (ArrayList<String> each : anagramlist.values()) {20             if (each.size() > 1) {21                 results.addAll(each);22             }23         }24         return results;25     }26 }

 

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.