关闭

LeetCode:Group Anagrams

标签: LeetCodeGroup Anagrams
2738人阅读 评论(0) 收藏 举报
分类:

Group Anagrams


Total Accepted: 76941 Total Submissions: 275801 Difficulty: Medium

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:

  1. For the return value, each inner list's elements must follow the lexicographic order.
  2. All inputs will be in lower-case.

Subscribe to see which companies asked this question

Hide Tags
 Hash Table String



























思路:

使用先将字符串数组排序(这样可以保证输出的顺序);

使用HashMap<String, List<String>>,key(String):是第一次出现的字符串;value(List<String>):用于保存所有相同字母乱序的值。


java code:

public class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        
        if(strs==null || strs.length==0) return new ArrayList<List<String>>();
        
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        Arrays.sort(strs);
        
        for(String str : strs) {
            char[] cs = str.toCharArray();
            Arrays.sort(cs);
            String keyStr = new String(cs);
            
            if(!map.containsKey(keyStr)) map.put(keyStr, new ArrayList<String>());
            map.get(keyStr).add(str);
        }
        
        return new ArrayList<List<String>>(map.values());
    }
}


0
0
查看评论

LeetCode(49)Group Anagrams

题目Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ [“ate”, “eat”,”tea”...
  • fly_yr
  • fly_yr
  • 2015-09-01 20:28
  • 5493

Leetcode: Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. For example: Input:  ["tea&...
  • doc_sgl
  • doc_sgl
  • 2013-10-05 17:02
  • 6930

LeetCode-49-Group Anagrams(哈希)-Medium

题意理解: 将给定字符串分组,要求同一组的字符串由相同字符组成,最终,将各组字符串按字典序输出; 题目分析: 1. 将字符串先进行排序,然后通过字符串比较来判定是否为同一组; 2. 将分组后的字符串集合再进行排序,从而满足题目的字典序要求; 3. 在分组的过程中需要进行查找,本题中...
  • eddy_liu
  • eddy_liu
  • 2016-01-10 18:32
  • 2006

LeetCode 49. Group Anagrams (Python)

题目描述: Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ ["ate"...
  • Lu_gee
  • Lu_gee
  • 2017-08-05 19:28
  • 262

[leetcode] 49. Group Anagrams 解题报告

题目链接:https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For example, given: ["eat", "t...
  • qq508618087
  • qq508618087
  • 2016-02-29 05:02
  • 1012

leetcode_c++:哈希:Group Anagrams (049)

Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ [“ate”, “eat”,”tea”],...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-07-02 14:06
  • 91

leetcode:Group Anagrams 【Java】

一、问题描述 Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "...
  • tterminator
  • tterminator
  • 2016-03-11 14:51
  • 1306

leetcode 49. Group Anagrams (Java实现)

原题: Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate"...
  • fyy607
  • fyy607
  • 2017-01-06 15:47
  • 340

Anagrams -- LeetCode

原题链接: http://oj.leetcode.com/problems/anagrams/  这是一道很经典的面试题了,在cc150里面也有,就是把一个数组按照易位构词分类。易位构词其实也很好理解,就是两个单词所包含的字符和数量都是一样的,只是顺序不同。 这个题简单的版本...
  • linhuanmars
  • linhuanmars
  • 2014-03-21 01:02
  • 16120

[LeetCode] Group Anagrams

Group Anagrams Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan"...
  • wangshaner1
  • wangshaner1
  • 2015-08-19 11:07
  • 2987
    个人资料
    • 访问:391466次
    • 积分:6805
    • 等级:
    • 排名:第4121名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论