zoukankan      html  css  js  c++  java
  • [LintCode] Anagrams

    Given an array of strings, return all groups of strings that are anagrams.  All inputs will be in lower case.

    What is Anagram?
    - Two strings are anagram if they can be the same after change the order of characters.

    Example

    Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"].

    Given ["ab", "ba", "cd", "dc", "e"], return ["ab", "ba", "cd", "dc"].

    Solution 1. Brute Force

    The most straightforward solution is to from left to right, fix a string, and compare it with all the remaining

    not-added strings on its right side. (By checking only the strings on its right side, we avoid the possiblity of

    adding duplicate strings.)If found any anagrams, add this string and its anagrams to the result list and mark

    them as added. Repeat this until we've checked all strings.

    This solution is not efficient, with a runtime of O(n^2 * k), where n is the number of strings, k is the average 

    length of each string.

    Solution 2. 

    For a string and all its possible anagram strings, we have k! different permutations. (assuming only distinct characters for simplicity)

    For a group of anagrams, if we fix one order, say ascending by sorting, we will only have to scan through the input list of strings 

    once.

    1. Create a hash map. Its keys are the ascending order of a string, its values are a list of all the anagrams of its keys.

    2. For each string, sort it, use this sorted string as key and add the unsorted string to the list of anagrams strings.

    3. Scan through the hash map and add all lists that have more than 1 string to the final result.

    This solution has a O(n * k * log k) runtime and O(n) space usage.

     1 public class Solution {
     2     public List<String> anagrams(String[] strs) {
     3         List<String> result = new ArrayList<String>();
     4         if(strs == null || strs.length == 0){
     5             return result;
     6         }
     7         HashMap<String, ArrayList<String>> hashmap = new HashMap<String, ArrayList<String>>();
     8         for(String s : strs){
     9             char[] chars = s.toCharArray();
    10             Arrays.sort(chars);
    11             String sortedStr = String.valueOf(chars);
    12             if(hashmap.containsKey(sortedStr)){
    13                 hashmap.get(sortedStr).add(s);
    14             }
    15             else{
    16                 ArrayList<String> list = new ArrayList<String>();
    17                 list.add(s);
    18                 hashmap.put(sortedStr, list);
    19             }
    20         }
    21         for(String s : hashmap.keySet()){
    22             ArrayList<String> list = hashmap.get(s);
    23             if(list.size() > 1){
    24                 result.addAll(list);    
    25             }
    26         }
    27         return result;
    28     }
    29 }

    Related Problems

    Strings Homomorphism

    Substring Anagrams

    Two Strings Are Anagrams

  • 相关阅读:
    hdu 1195 Open the Lock
    hdu 1180 诡异的楼梯
    hdu 1175 连连看
    背包(采药)
    LCS模板,求长度,并记录子串
    hdu 1159 Common Subsequence
    hdu 1231 最大连续子序列
    DP的简单应用
    hdu 1728 逃离迷宫
    点击页面其他地方,关闭弹框
  • 原文地址:https://www.cnblogs.com/lz87/p/6943162.html
Copyright © 2011-2022 走看看