LeetCode: Anagrams 解题报告

本文深入解析Anagrams算法,介绍如何通过使用Hashtable数据结构优化查找由相同字母组成的单词组,提供了一个高效的解决方案,避免了O(N^2)的时间复杂度。

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

Note: All inputs will be in lower-case.

思路:

建Hashtable,用排序过的string作为key,它的anagram作为ArrayList

这道题之前用暴力写的O(N^2)的TLE了,改用Hashtable来写
题目的意思是给一个String数组,找出其中由相同字母组成的单词。
例如:
S = ["abc", "bca", "bac", "bbb", "bbca", "abcb"]
答案为:
["abc", "bca", "bac", "bbca", "abcb"]
只有"bbb"没有相同字母组成的单词。

ref: http://blog.youkuaiyun.com/fightforyourdream/article/details/14217985

 1 public class Solution {
 2     public List<String> anagrams(String[] strs) {
 3         List<String> ret = new ArrayList<String>();
 4         
 5         if (strs == null) {
 6             return ret;
 7         }
 8         
 9         HashMap<String, List<String>> map = new HashMap<String, List<String>>();
10         
11         int len = strs.length;
12         for (int i = 0; i < len; i++) {
13             String s = strs[i];
14             
15             // Sort the string.            
16             char[] chars = s.toCharArray();
17             Arrays.sort(chars);
18             String strSort = new String(chars);   
19             
20             // Create a ArrayList for the sorted string.            
21             if (!map.containsKey(strSort)) {
22                 map.put(strSort, new ArrayList<String>());
23             }
24             
25             // Add a new string to the list of the hashmap.
26             map.get(strSort).add(s);
27         }
28         
29         // go through the map and add all the strings into the result.
30         for (Map.Entry<String, List<String>> entry: map.entrySet()) {
31             List<String> list = entry.getValue();
32             
33             // skip the entries which only have one string.
34             if (list.size() == 1) {
35                 continue;
36             }
37             
38             // add the strings into the list.
39             ret.addAll(list);
40         }
41         
42         return ret;
43     }
44 }
View Code

2015.1.3 redo:

 1 public class Solution {
 2     public List<String> anagrams(String[] strs) {
 3         List<String> ret = new ArrayList<String>();
 4         if (strs == null) {
 5             return ret;
 6         }
 7         
 8         HashMap<String, List<String>> map = new HashMap<String, List<String>>();
 9         for (int i = 0; i < strs.length; i++) {
10             String s = strs[i];
11             char[] chars = s.toCharArray();
12             
13             Arrays.sort(chars);
14             String sSort = new String(chars);
15             
16             if (map.containsKey(sSort)) {
17                 map.get(sSort).add(s);
18             } else {
19                 List<String> list = new ArrayList<String>();
20                 list.add(s);
21                 map.put(sSort, list);
22             }
23         }
24         
25         // Bug 1: should use map.entrySet() instead of MAP.
26         for (Map.Entry<String, List<String>> entry: map.entrySet()) {
27             List<String> list = entry.getValue();
28             if (list.size() > 1) {
29                 ret.addAll(list);
30             }
31         }
32         
33         return ret;
34     }
35 }
View Code

 

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/hash/Anagrams.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值