Third solution based on creating identical encoding for strings that are anagrams of each other for grouping.
(strs []string)
| 10 | // Third solution based on creating identical encoding for |
| 11 | // strings that are anagrams of each other for grouping. |
| 12 | func groupAnagrams(strs []string) [][]string { |
| 13 | groupMap := make(map[string][]string) |
| 14 | for _, s := range strs { |
| 15 | se := encodeString(s) |
| 16 | groupMap[se] = append(groupMap[se], s) |
| 17 | } |
| 18 | |
| 19 | groups := make([][]string, 0) |
| 20 | for _, v := range groupMap { |
| 21 | groups = append(groups, v) |
| 22 | } |
| 23 | |
| 24 | return groups |
| 25 | } |
| 26 | |
| 27 | func encodeString(s string) string { |
| 28 | chars := make([]int, 26) |