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.csdn.net/fightforyourdream/article/details/14217985

 public class Solution {
public List<String> anagrams(String[] strs) {
List<String> ret = new ArrayList<String>(); if (strs == null) {
return ret;
} HashMap<String, List<String>> map = new HashMap<String, List<String>>(); int len = strs.length;
for (int i = ; i < len; i++) {
String s = strs[i]; // Sort the string.
char[] chars = s.toCharArray();
Arrays.sort(chars);
String strSort = new String(chars); // Create a ArrayList for the sorted string.
if (!map.containsKey(strSort)) {
map.put(strSort, new ArrayList<String>());
} // Add a new string to the list of the hashmap.
map.get(strSort).add(s);
} // go through the map and add all the strings into the result.
for (Map.Entry<String, List<String>> entry: map.entrySet()) {
List<String> list = entry.getValue(); // skip the entries which only have one string.
if (list.size() == ) {
continue;
} // add the strings into the list.
ret.addAll(list);
} return ret;
}
}

2015.1.3 redo:

 public class Solution {
public List<String> anagrams(String[] strs) {
List<String> ret = new ArrayList<String>();
if (strs == null) {
return ret;
} HashMap<String, List<String>> map = new HashMap<String, List<String>>();
for (int i = 0; i < strs.length; i++) {
String s = strs[i];
char[] chars = s.toCharArray(); Arrays.sort(chars);
String sSort = new String(chars); if (map.containsKey(sSort)) {
map.get(sSort).add(s);
} else {
List<String> list = new ArrayList<String>();
list.add(s);
map.put(sSort, list);
}
} // Bug 1: should use map.entrySet() instead of MAP.
for (Map.Entry<String, List<String>> entry: map.entrySet()) {
List<String> list = entry.getValue();
if (list.size() > 1) {
ret.addAll(list);
}
} return ret;
}
}

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

LeetCode: Anagrams 解题报告的更多相关文章

  1. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  2. 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...

  3. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  4. LeetCode C++ 解题报告

    自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...

  5. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  6. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  7. LeetCode: Triangle 解题报告

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  8. LeetCode: isSameTree1 解题报告

    isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...

  9. LeetCode: Combinations 解题报告

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...

随机推荐

  1. 函数响应式编程(FRP)框架--ReactiveCocoa

    由于工作原因,有段时间没更新博客了,甚是抱歉,只是,从今天開始我又活跃起来了,哈哈,于是决定每周更新一博.大家互相学习.交流. 今天呢.讨论一下关于ReactiveCocoa,这个採用函数响应式编程( ...

  2. 【laravel54】详解中间件

    1.中间件定义:对http请求进行一层过滤,通过过滤才能继续执行请求 2.中间件方法handle方法参数详解: 其中参数的形式可以有多个,使用[,]进行分割. 3.路由中使用中间件: 3.1 中间件使 ...

  3. nginx error: upstream prematurely closed connection while reading response header from upstream

    本篇文章由:http://xinpure.com/nginx-error-upstream-prematurely-closed-connection-while-reading-response-h ...

  4. poj-------Common Subsequence(poj 1458)

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34477   Accepted: 13 ...

  5. java IO 入门例子

    import java.io.File; /** * File类型基本操作 */ /** * @author Administrator * */ public class FileDemo { /* ...

  6. 《JAVA与模式》之有感

    陆陆续续看了gof,大话设计模式等books,对于设计模式仍然是徘徊于门外,随偶有感悟,然久必忘记,是实则悟性太低. 因此作此文,结合博客中一系列关于设计模式的博文,加深对设计模式的理解,同时备自己随 ...

  7. 使用Apktools反编译apk应用

    使用Apktools反编译apk应用 1.获取APK的classes.dex文件: 得到你想要的应用的apk文件,用解压软件打开apk,从apk中复制出classes.dex文件. 2.classes ...

  8. Python直接赋值、浅拷贝和深度拷贝解析

    直接赋值:其实就是对象的引用(别名). 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 ...

  9. Python 元组 tuple() 方法

    描述 Python 元组 tuple() 方法用于将可迭代对象(字符串.列表.元祖.字典)转换为元组. 语法 tuple() 方法语法: tuple(iterable) 参数 iterable -- ...

  10. Python abs() 函数

    描述 abs() 函数返回数字的绝对值. 语法 以下是 abs() 方法的语法: abs( x ) 参数 x -- 数值表达式,可以是整数,浮点数,复数. 返回值 函数返回 x(数字)的绝对值,如果参 ...