leetcode:Group Anagrams 【Java】

一、问题描述

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.

二、问题分析

把字符串中的字符排序后,若都相等则为同一组。

不能把字符串中的字符ascii码相加后得到sum,若sum相等则为一组,反例:"duh","ill",sum = 321。

三、算法代码

public class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        for (String s : strs) {
            char [] str2char = s.toCharArray();
            Arrays.sort(str2char);
            String sortedStr = String.valueOf(str2char);
            if(map.containsKey(sortedStr)){
                map.get(sortedStr).add(s);
            }else{
                ArrayList<String> tmp = new ArrayList<String>();
                tmp.add(s);
                map.put(sortedStr, tmp);
            }
        }
        List<List<String>> result = new ArrayList<List<String>>();
        for (List<String> list : map.values()) {
            Collections.sort(list);
            result.add(list);
        }
        return result;
    }
}


你可能感兴趣的:(LeetCode)