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

Note: All inputs will be in lower-case.

他的意思就是回文构词法,即单词里的字母的种类和数目没有改变,仅仅是改变了字母的排列顺序。
input= ["abc", "bca", "bac", "bbb", "bbca", "abcb"]
output=["abc", "bca", "bac", "bbca", "abcb"]
我们仅仅须要一个HashMap就能够。

public class Solution {
public List<String> anagrams(String[] strs) {
ArrayList<String> result = new ArrayList<String>();
HashMap<String, String> hm = new HashMap<String, String>();
for (String str : strs) {
char[] c = str.toCharArray();
Arrays.sort(c);
if (!hm.containsKey(String.valueOf(c))) {
hm.put(String.valueOf(c), str);
} else {
String s = hm.get(String.valueOf(c));
if (!result.contains(s))//某个回文序列第一次出现的单词,
//我们并没有加入,如今补上
result.add(s);
result.add(str);
}
}
return result;
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode 48 Anagrams的更多相关文章

  1. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  2. 前端与算法 leetcode 48. 旋转图像

    目录 # 前端与算法 leetcode 48. 旋转图像 题目描述 概要 提示 解析 解法一:转置加翻转 解法二:在单次循环中旋转 4 个矩形 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  3. [LeetCode] Group Anagrams 群组错位词

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  4. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. 【leetcode】Anagrams

    Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  6. 【leetcode】Anagrams (middle)

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  7. Java for LeetCode 049 Anagrams

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  8. Leetcode#49 Anagrams

    原题地址 Anagram:变位词.两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同 第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么..后来还是看网上其他人的总结知道是怎么 ...

  9. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

随机推荐

  1. python学习笔记之二:使用字符串

    这里会介绍如何使用字符串格式化其他的值,并了解一下利用字符串的分割,连接,搜索等方法能做些什么. 1.基本字符串操作 所有标准的序列操作(索引,分片,乘法,判断成员资格,求长度,取最大值和最小值)对字 ...

  2. macbook连接linuxserver后不显示matlab桌面[问题]

    macbook 登录远程linuxserver.Macbook今天,系统版本号10.9.4.这是更新,打开matlab当提醒x11不存在.一个接着安装Xquarz2.7.6. matlab它可以在本地 ...

  3. centos6.5安装nodejs

    Preface(前言) 一次偶然的机会知道有nodejs这个东西,确实对它还是非常感兴趣的.刚開始仅仅知道它能让javascript写后台,然后前后台都由javascript来写,确实认为真的挺爽,毕 ...

  4. Portal.MVC

    Portal.MVC Portal.MVC 简介 项目是基于MVC4+EF,带有角色,权限,用户中心及账户相关(登录,注册,修改密码,找回密码等)等基本功能.参考的开源项目nopcommerce,这是 ...

  5. c++堆栈实现

    A Stack is a data-structure that You can only add an element to the top of the Stack, andYou can onl ...

  6. AccountManager教程

    API阅读 此类提供所述用户接口到集中登记帐户. 用户只需输入一次帐号password后,您将能够访问internet资源. 不同的在线服务用不同的方式来管理用户,所以account manager ...

  7. 使用 WPF 创建单实例应用程序

    一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...

  8. 系统负载测试工具-LoadRunner

    LoadRunner的主要作用是对系统压力测试进行分析 与之相类似的工具是:badboy:录制脚本工具+jmeter:分析结果工具

  9. (转)mvn clean install 与 mvn install 的区别(为啥用clean)

    之前写代码的过程中曾经遇到过问题,用mvn install后,新改的内容不生效,一定要后来使用mvn clean install 才生效,由于之前没有做记录,以及记不清是什么情况下才会出现的问题,于是 ...

  10. 《数据结构、算法及应用》9.(C++实施订单)

    最近阅读<数据结构.算法及应用>这本书,书中的习题汇总,用自己的方法来实现这些问题.可能效率.等方面存在着非常多的问题,也可能是错误的实现.假设大家在看这本书的时候有更优更好的方法来实现, ...