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 }
  • 相关阅读:
    第七次作业-正规式到正规文法与自动机
    第六次作业——正规文法与正规式
    作业5 词法分析程序的设计与实现
    作业4—文法和语言总结与梳理
    第三次作业
    第二次作业-语言和文法
    编译原理第一次作业
    记录在腾讯云上搭建Ubuntu服务器
    第八章总结--排序 数据结构课程终章
    第七章-查找
  • 原文地址:https://www.cnblogs.com/feiling/p/3243128.html
Copyright © 2011-2022 走看看