题目:

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

Note: All inputs will be in lower-case.

题解:

这道题看所给的字符串数组里面有多少个是同一个变形词变的。这道题同样使用HashMap来帮助存老值和新值,以及帮忙判断是否是变形词。

首先对每个string转换成char array然后排下序,HashMap里面的key存sort后的词,value存原始的词。然后如果这个排好序的词没在HashMap中出现过,那么就把这个sorted word和unsortedword put进HashMap里面。如果一个sorted word是在HashMap里面存在过的,说明这个词肯定是个变形词,除了把这个词加入到返回结果中,还需要把之前第一个存进HashMap里面的value存入result中。

代码如下:

 1  public ArrayList<String> anagrams(String[] strs) {
 2      ArrayList<String> result=new ArrayList<String>();
 3      
 4      if (strs==null||strs.length==0)
 5          return result;
 6      
 7      HashMap<String,ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
 8      
 9      for (String s:strs){
          char[] temp=s.toCharArray();
          Arrays.sort(temp);
          String tempStr=new String(temp);
          
          if (hm.containsKey(tempStr)){
              if(hm.get(tempStr).size() == 1)
                 result.add(hm.get(tempStr).get(0));
              hm.get(tempStr).add(s);
              result.add(s);
          }else{
              ArrayList<String> tempList=new ArrayList<String>();
              tempList.add(s);
              hm.put(tempStr, tempList);
              }
         }
         return result;
  }

Anagrams leetcode java的更多相关文章

  1. Group Anagrams - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Group Anagrams - LeetCode 注意点 字母都是小写的 解法 解法一:用一个字符串表示strs[i]中出现的字母,比如:abc-> ...

  2. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  3. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  4. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  5. 49. Group Anagrams - LeetCode

    Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...

  6. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  7. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. Anagrams [LeetCode]

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

随机推荐

  1. 【知了堂学习笔记】java 编写几种常见排序算法3

    排序的分类: 1.希尔排序 希尔排序是快速插入排序的改进版,希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序:随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰 ...

  2. python创建有序字典OrderedDict()

    python 有序字典OrderedDict # -*- coding:utf-8 -*- """ python有序字典 需导入模块collections "& ...

  3. Oracle 默认的几个登陆用户名和密码

    默认用户有这么几个,system,sys,scott,hr ,一般scott 和hr 作为你的练习用户.system的默认密码是 manager sys的默认密码是 change_on_install ...

  4. JDK源码学习笔记——Enum枚举使用及原理

    一.为什么使用枚举 什么时候应该使用枚举呢?每当需要一组固定的常量的时候,如一周的天数.一年四季等.或者是在我们编译前就知道其包含的所有值的集合. 利用 public final static 完全可 ...

  5. bzoj 3252: 攻略 -- 长链剖分+贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MB Description 题目简述:树版[k取方格数]   众所周知,桂木桂马是攻略之神,开启攻略之神 ...

  6. URAL 1880 Psych Up's Eigenvalues

    1880. Psych Up's Eigenvalues Time limit: 0.5 secondMemory limit: 64 MB At one of the contests at the ...

  7. js面向对象写页面

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. linux下TP5安装好Workerman 报错:Class 'think\worker\Server' not found

    今天把功能放到服务器,本地测试正常,上传到服务器上报错Class 'think\worker\Server' not found 首先想到的是Windows和Linux下大小写的问题,查看了代码,并没 ...

  9. fragment和fragmentactivity解析

    一.为什么要使用Fragment  1.当我们须要动态的多界面切换的时候,就须要将UI元素和Activity融合成一个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单个 ...

  10. mysql 3.2.49 源代码安装-redhat 5 x64

    [mysql@localhost ~]$ uname -r2.6.32 [root@localhost ~]#cp /usr/include/pthread.h /usr/include/pthrea ...