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 }

 

转载于:https://www.cnblogs.com/feiling/p/3243128.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值