zoukankan      html  css  js  c++  java
  • leetcode -- Anagrams

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

    Note: All inputs will be in lower-case.

    Input:  ["tea","and","ate","eat","den"]

    Output:   ["tea","ate","eat"]

    ["",""]------------->["",""]

    ["","",""]---------->["","",""]

    ["tea","and","ate","eat","dan"]------------>["and","dan","tea","ate","eat"]

    TLE

     1 public ArrayList<String> anagrams(String[] strs) {
     2         // Start typing your Java solution below
     3         // DO NOT write main() function
     4         int len = strs.length;
     5         
     6         Map<Integer, String> sorted = new HashMap<Integer, String>();
     7         
     8         sortStrs(strs, sorted, len);
     9         ArrayList<String> result = new ArrayList<String>();
    10         boolean[] added = new boolean[len];
    11         for(int i = 0; i < len; i++)
    12             added[i] = false;
    13         
    14         for(int i = 0; i < len; i++){
    15             String tmp = sorted.get(i);
    16             boolean find = false;
    17             for(int j = i + 1; j < len; j++){
    18                 if(!added[j] && tmp.equals(sorted.get(j))){
    19                     result.add(strs[j]);
    20                     added[j] = true;
    21                     find = true;
    22                 }
    23             }
    24             if(!added[i] && find){
    25                 result.add(strs[i]);
    26                 added[i] = true;
    27                 //return result;
    28             }
    29         }
    30         return result;
    31     }
    32     
    33     public void sortStrs(String[] strs, Map<Integer, String> sorted, int len){
    34         for(int i =0; i < len; i++){
    35             String s = strs[i];
    36             char[] c = s.toCharArray();
    37             Arrays.sort(c);
    38             sorted.put(i, new String(c));
    39         }
    40     }

    Solution and Precautions:

    First, note what is Anagrams, you can google search it and let’s claim that word A and B is called anagram to each other if we can get word B(A) from A(B)  by rearranging the letters of A(B) to produce B(A), using all the original letters exactly once.

    Then, one straightforward way is to compare each pair of words and to see if they are anagrams, my first try is just like this, starting from the first word, I search through all the remaining words and get its corresponding anagrams (to the first word), of course, all such anagrams we found for the first word won’t be anagrams to any other words so we don’t have to consider them for further check. This method could pass the small data set test but will get TLE for large dataset test. The time complexity is O(N^2 * K log K) where N is the number of words and K is the length of the longest word, or you may say the average length of the words.

    Finally, if we think deeper into this, we will find that we actually don’t have to do the N^2 comparisons at all. The key observation is that A and B is anagram to each other if and only if their sorted form are exactly the same. As a result, one linear scan through the words list is enough, for each word we can get its sorted form in K log K time, and we can use map to store the groups of words which are in the same sorted form. The time complexity is O(N K log K), this approach could pass both small data test and large data test.

     1 public class Solution {
     2     public ArrayList<String> anagrams(String[] strs) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         int len = strs.length;
     6         ArrayList<String> result = new ArrayList<String>();
     7         Map<String, ArrayList<String>> sorted = new HashMap<String, ArrayList<String>>();
     8         
     9         for(int i = 0; i < len; i++){
    10             String tmp = strs[i];
    11             String sort = sortStr(tmp);
    12             if(sorted.get(sort) != null){
    13                 sorted.get(sort).add(tmp);
    14             } else {
    15                 ArrayList<String> list = new ArrayList<String>();
    16                 list.add(tmp);
    17                 sorted.put(sort, list);
    18             }
    19         }
    20         
    21         Iterator iter = sorted.values().iterator();
    22         while(iter.hasNext()){
    23             ArrayList<String> list = (ArrayList<String>)iter.next();
    24             if(list.size() > 1){
    25                 result.addAll(list);
    26             }
    27         }
    28         
    29         return result;
    30     }
    31     
    32     public String sortStr(String str){
    33         char[] c = str.toCharArray();
    34         Arrays.sort(c);
    35         return new String(c);
    36     }
    37 }
  • 相关阅读:
    Win10家庭版、专业版、企业版、教育版各版本功能区别对照表
    BaseDAL最牛数据层基类2
    C# 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
    IIS下众多网站,如何快速定位某站点日志在哪个文件夹?
    【进阶技术】一篇文章搞掂:OAuth2
    【进阶技术】一篇文章搞掂:RibbitMQ
    【系统架构理论】一篇文章搞掂:设计模式
    【前端技术】一篇文章搞掂:WeX5
    【c#技术】一篇文章搞掂:Newtonsoft.Json Json.Net
    【Java架构:持续交付】一篇文章搞掂:Jenkins
  • 原文地址:https://www.cnblogs.com/feiling/p/3243128.html
Copyright © 2011-2022 走看看