题目:

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. GPS数据包格式及数据包解析

    GPS数据包解析 GPS数据包解析 目的 GPS数据类型及格式 数据格式 数据解释 解析代码 结构体定义 GPRMC解析函数 GPGGA解析函数 测试样例输出 gps数据包格式 gps数据解析 车联网 ...

  2. 试水jdk8 stream

    jdk8出来日子不短了,jdk11都出来了,不过用的最多的不过是1.5罢了. 今年终于鼓起勇气认真对待它,在18年记录下学习stream,画上一个圆. 先看个图 Java8中有两大最为重要的改变.第一 ...

  3. KVM libvirt的CPU热添加

    一. NUMA1. NUMA 介绍    早期的时候,每台服务器都是单CPU,随着技术的发展,出现了多CPU共同工作的需求.    NUMA(Non-Uniform Memory Access,非一致 ...

  4. ASP.net 简单分页的实现

    在自己的项目中有一个文章的管理页面需要用到分页, 这种分页方法是在黑马的一个视频中看到的,便用在了自己的项目中. 但是使用控件实在是太丑,虽然我写的也丑....... gridview 控件提供的分页 ...

  5. 【Codeforces 498B】 B. Name That Tune (概率DP)

    B. Name That Tune time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. 【HDU 3662】3D Convex Hull

    http://acm.hdu.edu.cn/showproblem.php?pid=3662 求给定空间中的点的三维凸包上有多少个面. 用增量法,不断加入点,把新加的点能看到的面都删掉,不能看到的面与 ...

  7. 转MySQL常见错误分析与解决方法总结

    一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分析:这说明“localhost”计算机 ...

  8. [COGS 2066]七十与十七

    http://218.28.19.228/cogs/problem/problem.php?pid=2066 [题目描述] 七十君最近爱上了排序算法,于是Ta让十七君给Ta讲冒泡排序. 十七君给七十君 ...

  9. libuuid.so: cannot open shared object file: No such file or directory

    在玩ngx-lua时候有个 resty-uuid 需要引用 libuuid.so 动态库 打印log提示信息是这样的: libuuid.so: cannot open shared object fi ...

  10. Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和

    B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...