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. (PMP)第5章-----项目范围管理

    产品范围:所具有的特征和功能 项目范围:必须完成的工作. 5.1 规划范围管理 输入 工具与技术 输出 1.项目章程 2.项目管理计划 (质量管理计划, 项目生命周期描述, 开发方法) 3.事业环境因 ...

  2. 1034 Head of a Gang 图的遍历,map使用

    命名冲突,导致编译失败.这大概就是之前看到的最好不要using namespace std:的原因

  3. PHP单一文件入口框架简析

    <?php /** * PHP单一文件框架设计简析 * 1.MVC架构实现 * 2.URL路由原理 */ //URL路由原理 /** * 路由作用 * 获取url中的c和a变量,执行c类对应的方 ...

  4. python_flask 基础巩固(自定义URL转换器)

    自定义URL转换器(在BaseConverter类外定义)from werkzeug.routing import BaseConverter定义类继承BaseConverter 实现类app.url ...

  5. Python基础-if判断以及其他循环

    if else 判断: #!/usr/bin/env python # -*- coding:utf-8 -*- import getpass name = input("用户名??&quo ...

  6. document,element,node方法

    document方法: getElementById(id)                             返回指定结点的引用 getElementsByTagName_r(name)    ...

  7. docker 2(local registry)

    1.获取仓库镜像 ,sudo docker pull registry 2.sudo vim /etc/init/docker.conf 增加--insecure-registry IP:5000 3 ...

  8. Spring-Data-Jpa环境配置与实际应用

    上次我们讲述了<Spring-Data-Jpa概述与接口>,接下来我们再讲讲Spring-Data-Jpa环境配置与实际应用. Spring-Data 方法定义规范与使用配置 简单条件查询 ...

  9. 微博第三方登录使用social_django实现显示登陆的用户名

    首先修改social_soce源码,将用户信息添加进cookie 将其修改为:  response =  backend.strategy.redirect(url)    payload = jwt ...

  10. .net core 发布linux报错“The configured user limit (128) on the number of inotify instances has been reached”

    https://stackoverflow.com/questions/45875981/error-while-reading-json-file-in-dotnet-core-the-config ...