Java Solutions
Solution 1: Group by Sorted String\n\n> Two strings are anagrams if and only if their sorted strings are equal.\n- We use Map to group the strings by their sort
**Solution 1: Group by Sorted String**\n\n> Two strings are `anagrams` if and only if their sorted strings are equal.\n- We use `Map` to group the strings by their sorted counterparts. We use the sorted string as the key and List of anagram strings as the value. Each key `keyStr` is a sorted string, and each value is the list of strings from the initial input that when sorted, are equal to `keyStr`.\n- The outer loop has complexity `O(n)` as we iterate through each string. Then, we sort each string in `O(k log k)` time.\n\nTime complexity: `O(n * k log k)` - where `n` is the length of input array `strs`, and `k` is the maximum length of a string in `strs` array. \nSpace complexity: `O(n * k)` - the total information content stored in a `Map`. \n\n```\nclass Solution\n{\n    public List<List<String>> groupAnagrams(String[] strs)\n\t{\n\t\tMap<String, List<String>> map = new HashMap<>();\n\t\t\n        for(String s : strs)\n\t\t{\n            char[] ca = s.toCharArray();\n            Arrays.sort(ca);\n            String keyStr = String.valueOf(ca);\n    \t\tmap.putIfAbsent(keyStr, new ArrayList<>());\t\t// If no mapping for a key, then map a new key with an empty list.\n\t\t\tmap.get(keyStr).add(s);\n        }\n        return new ArrayList<>(map.values());\n    }\n}\n```\n\n**Solution 2: Group by Character Counts**\n\n> Two strings are `anagrams` if and only if their character counts are the same.\n- We can transform each string `s` into an encoded string based on the its character counts.\n- For example, string `abbccc` will be `a1b2c3d0e0...z0` where there are `26` entries for each character from `a` to `z`.\n\nTime complexity: `O(n * k)` - Encoding each string is linear in the size of the string which is `O(k)`, and we encode every string. So `O(n) * O(k) = O(n * k)`.\nSpace complexity: `O(n * k)` - the total information content stored in a `Map`.\n\n```\nclass Solution\n{\n    public List<List<String>> groupAnagrams(String[] strs)\n\t{\n        Map<String, List<String>> map = new HashMap<>();\n\t\t\n\t\tfor(String s : strs)\n\t\t{\n            String keyStr = encodeString(s);\n            map.putIfAbsent(keyStr, new ArrayList<>());\n            map.get(keyStr).add(s);\n        }\n        return new ArrayList<>(map.values());\n    }\n\n\tprivate String encodeString(String s)\n\t{\n\t\tStringBuilder sb = new StringBuilder();\n\t\tint[] count = new int[26];\n\t\tArrays.fill(count, 0);\n\n\t\tfor(char c : s.toCharArray())\n\t\t{\n\t\t\tcount[c - \'a\'] += 1;\n\t\t}\n\n\t\tfor(int i = 0; i < 26; i++)\n\t\t{\n\t\t\tsb.append(i + \'a\').append(count[i]);\n\t\t}\n\t\treturn sb.toString();\n\t}\n}\n```\n\n**Solution 3: Group by hashCode**\n\n- Here we compute the `hashCode` of an array of character counts of the string.\n\nTime complexity: `O(n * k)` - Computing a hashCode for each string is linear in the size of the string which is `O(k)`, and we do this for every string. So `O(n) * O(k) = O(n * k)`.\nSpace complexity: `O(n * k)` - the total information content stored in a `Map`.\n\n```\nclass Solution\n{\n    public List<List<String>> groupAnagrams(String[] strs)\n\t{\n        Map<Integer, List<String>> map = new HashMap<>();\n\t\t\n\t\tfor(String s : strs)\n\t\t{\n            int hashCode = computeHashCode(s);\n            map.putIfAbsent(hashCode, new ArrayList<>());\n            map.get(hashCode).add(s);\n        }\n        return new ArrayList<>(map.values());\n    }\n\n\tprivate int computeHashCode(String s)\n\t{\n\t\tint[] count = new int[26];\n\n\t\tfor(char c : s.toCharArray())\n\t\t{\n\t\t\tcount[c - \'a\'] += 1;\n\t\t}\n\t\treturn Arrays.hashCode(count);\n\t}\n}\n```
[]