题目:

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. 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]

    题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...

  2. linux 下安装jdk环境安装

    一.创建jdk安装目录mkdir /usr/local/java 二.将jdk解压到安装目录中,直接到java目录中,如果不是处理下不要有子目录 tar -zxvf jdk-8u91-linux-x6 ...

  3. Error after SQL Server 2012 installation: Login Failure for "SQL Server Integration Services 11.0" SSIS service

    When you install SQL Server 2012 and you try to connect to SSIS services, you cannot due to that the ...

  4. Highmaps网页图表教程之Highmaps第一个实例与图表构成

    Highmaps网页图表教程之Highmaps第一个实例与图表构成 Highmaps第一个实例 下面我们来实现本教程的第一个Highmaps实例. [实例1-1:hellomap]下面来制作一个中国地 ...

  5. 【UOJ #221】【NOI 2016】循环之美

    http://uoj.ac/problem/221 因为\(a\)和\(b\)不互质时,\(\frac ab=\frac{\frac a{(a,b)}}{\frac b{(a,b)}}\),所以只用求 ...

  6. 20162307 课堂测试 hash

    20162307 课堂测试 hash 作业要求 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85, ...

  7. BZOJ3473 字符串 广义后缀自动机

    今天主攻了下SAM 好多东西以前都没理解到 对于这道题 我们建一个自动机存所有串 每个穿last从1开始 对于自动机上每个点额外记一个cnt 表示能匹配到这个点的不同串个数 建完对每个串在自动机上匹配 ...

  8. python开发_tkinter_菜单的不同选项

    python的tkinter模块中,菜单也可以由你自定义你的风格 下面是我做的demo 运行效果: ====================================== 代码部分: ===== ...

  9. brainfuck 解释器

    #include <cstdio>#include <cmath>#include <cstring>#include <ctime>#include ...

  10. Codeforces Round #281 (Div. 2) C. Vasya and Basketball 暴力水题

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...