LeetCode

Group Anagrams

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.

Java

public class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> rst = new ArrayList<List<String>>();
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        Arrays.sort(strs);

        for(int i = 0; i < strs.length; i++) {
            char[] strChar = strs[i].toCharArray();
            Arrays.sort(strChar);
            String str = new String(strChar);
            if(map.containsKey(str)) {
                map.get(str).add(strs[i]);
            }
            else {
                List<String> list = new ArrayList<String>();
                list.add(strs[i]);
                map.put(str, list);
            }
        }

        for(List<String> val : map.values()) {
            rst.add(val);
        }

        return rst;
    }
}