import java.util.*;

/**
*
* Source : https://oj.leetcode.com/problems/anagrams/
*
* Created by lverpeng on 2017/7/18.
*
* Given an array of strings, group anagrams together.
*
* For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
* Return:
*
* [
* ["ate", "eat","tea"],
* ["nat","tan"],
* ["bat"]
* ]
*
* Note:
*
* For the return value, each inner list's elements must follow the lexicographic order.
* All inputs will be in lower-case.
*
* Update (2015-08-09):
* The signature of the function had been updated to return list<list<string>> instead
* of list<string>, as suggested here. If you still see your function signature return
* a list<string>, please click the reload button to reset your code definition.
*
*/
public class GroupAnagram { public List<String[]> anagram (String[] strArr) {
List<String[]> result = new ArrayList<String[]>();
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
for (int i = 0; i < strArr.length; i++) {
char[] charArr = strArr[i].toCharArray();
Arrays.sort(charArr);
String str = new String(charArr);
if (map.keySet().contains(str)) {
map.get(str).add(i);
} else {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
map.put(str, list);
}
}
for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
String[] strs = new String[entry.getValue().size()];
int index = 0;
for (Integer i : entry.getValue()) {
strs[index] = strArr[entry.getValue().get(index)];
index ++;
}
Arrays.sort(strs);
result.add(strs);
}
return result;
} public static void printList (List<String[]> list) {
for (String[] strs : list) {
System.out.println(Arrays.toString(strs));
}
System.out.println();
} public static void main(String[] args) {
GroupAnagram groupAnagram = new GroupAnagram();
printList(groupAnagram.anagram(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"})); }
}

leetcode — anagrams的更多相关文章

  1. [LeetCode] Anagrams 错位词

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

  2. [leetcode]Anagrams @ Python

    原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of ...

  3. Leetcode: Anagrams(颠倒字母而成的字)

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

  4. LeetCode——Anagrams

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

  5. Leetcode Anagrams

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

  6. LeetCode ---Anagrams() 详解

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

  7. LeetCode Anagrams My solution

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

  8. LeetCode: Anagrams 解题报告

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

  9. [Leetcode] Anagrams 颠倒字母构成词

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

随机推荐

  1. a,b值进行交换的方法

    方法一 最容易想到的就是拿一个空“杯子”进行转换,代码如下: <script type="text/javascript"> ; ; var c; c = a; a = ...

  2. 【翻译】Flume 1.8.0 User Guide(用户指南)

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  3. 贝叶斯公式52张牌猜黑桃A策略

    贝叶斯公式52张牌猜黑桃A策略 考虑有208平行世界,其中有4个世界(1/52)的黑桃A方在第一张牌的位置,余下204个世界中,有4个世界的黑桃A在第2张牌的位置,4个世界在第3张牌的位置..... ...

  4. openmp入门总结

    Ref: https://wdxtub.com/2016/03/20/openmp-guide/ 简介 这门课作为 ECE 中少有的跟计算机科学相关的课,自然是必上不可.不过无论是 OpenMP 还是 ...

  5. Day11 (黑客成长日记) 爬取网站图片

    #导入第三方库# coding:utf-8import requests,re #找到需要爬取的网站'http://www.qqjia.com/sucai/sucai1210.htm' #1>获 ...

  6. TensorFlow学习笔记1

    1.daocloud 拉取 docker镜像 docker pull daocloud.io/daocloud/tensorflow:latest 镜像位置: /Users/{YourUserName ...

  7. PopupWindow计算弹出位置

    1.首先自定义PopupWindow    popWindowView= LinearLayout.inflate(context, R.layout.popupWindow,null);    po ...

  8. Eclipse项目里面看源码和文档

    Eclipse项目里面看源码 1.新建项目列表 2.进入struts2-core-2.3.20.jar,双击之后,看不到源码 3.右键struts2-core-2.3.20.jar,选择propert ...

  9. 【react】利用shouldComponentUpdate钩子函数优化react性能以及引入immutable库的必要性

    凡是参阅过react官方英文文档的童鞋大体上都能知道对于一个组件来说,其state的改变(调用this.setState()方法)以及从父组件接受的props发生变化时,会导致组件重渲染,正所谓&qu ...

  10. 友链 & 日记

    友链 & 日记 关于 \(Owen\) 温州中学初三 \(OIer\),目前 \(OI\) 水平一般,文化课成绩浮在中游.喜欢二次元,喜欢听音乐,标准宅一枚.虽然入宅时间很短 欢迎大家跟 \( ...