Hash solution with explanation | Smart solution | O(n*m)
Intuition\nIn the problem, it is said that we need to group anagrams.\nThe easiest way to group anything is by creating a map and putting all the values by keys
# Intuition\nIn the problem, it is said that we need to group anagrams.\nThe easiest way to group anything is by creating a map and putting all the values by keys. Since the strings can be different but belong to one group, it is needed to create a hash algorithm for anagrams that will produce the same hash for `abc` and `cba`.\n\n# Approach\nA hashing algorithm must be created to generate the same keys for equal anagrams.\nTwo anagrams are equal if the number of each letter in the strings is equal. For example, `aabbc` is equal to `babac` if the `amount of [a, b, c]` is the same in both strings. In this case, the strings are equal because, `a == 2`, `b == 2`, and `c == 1` in both strings.\nIn the description of the problem, it is specified that the strings can be only lowercase English letters, there are 26 letters in the English alphabet.\nSince two anagrams are equal only if the numbers of their letters are equal, the key can be the counter for each letter in the string.\n`type Key [26]byte` represents the counter of the amount of each letter in the string.\nFor example, for `aabbbcccc` the key will be `a:2,b:3,c:4,e:0,...,z:0`.\n\nTo generate a key from a string the `strKey` function can be used. Since all the letters are numbers according to the ASCII table and `\'a\'` is `97`, the function must subtract `\'a\'` from the `str[i]` to get the index of the letter. For example, `\'c\'` is `99`, so `\'c\' - \'a\' == 99 - 97 == 2`.\n\nOnce the hash function is written, all is left is to group the strings by their hashes.\n```\n\tfor _, v := range strs {\n\t\tkey := strKey(v)\n\t\tgroups[key] = append(groups[key], v)\n\t}\n```\n\nAnd after this convert the map of slices of strings to a 2D slice of strings and return it.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`O(n)` where `n` is the number of all letters in the `strs` slice.\n\n# Code\n```\ntype Key [26]byte\n\nfunc strKey(str string) (key Key) {\n\tfor i := range str {\n\t\tkey[str[i]-\'a\']++\n\t}\n\treturn\n}\n\nfunc groupAnagrams(strs []string) [][]string {\n\tgroups := make(map[Key][]string)\n\n\tfor _, v := range strs {\n\t\tkey := strKey(v)\n\t\tgroups[key] = append(groups[key], v)\n\t}\n\n\tresult := make([][]string, 0, len(groups))\n\tfor _, v := range groups {\n\t\tresult = append(result, v)\n\t}\n\treturn result\n}\n```
['Go']