Group Anagrams

Group Anagrams hash table

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: All inputs will be in lower-case.

Group Anagrams Solution

public class GroupAnagrams {
    public List<List<String>> groupAnagrams(String[] strs) {
        if(strs == null || strs.length == 0) {
            return new ArrayList<List<String>>();
        }

        HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();

        Arrays.sort(strs);

        for(String s : strs) {
            char[] characters = s.toCharArray();

            Arrays.sort(characters);

            String key = String.valueOf(characters);

            if(!map.containsKey(key)) {
                map.put(key, new ArrayList<String>());
            }

            map.get(key).add(s);
        }

        return new ArrayList<List<String>>(map.values());
    }
}
Last modified October 4, 2020