-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_anagrams.go
57 lines (48 loc) · 1.06 KB
/
group_anagrams.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// https://leetcode.com/problems/group-anagrams/
package group_anagrams
import (
"crypto/sha1"
"sort"
)
func groupAnagrams(strs []string) [][]string {
hashes := getHashes(strs)
indexes := make(map[[20]byte][]int)
for i := range strs {
h := hashes[i]
if v, ok := indexes[h]; !ok {
indexes[h] = []int{i}
} else {
indexes[h] = append(v, i)
}
}
groups := make([][]string, len(indexes))
i := 0
for _, v := range indexes {
groups[i] = make([]string, len(v))
for j := 0; j < len(v); j++ {
idx := v[j]
groups[i][j] = strs[idx]
}
i++
}
return groups
}
// Count numbers of letters for each word O(N)
func getHashes(strs []string) [][20]byte {
sorted := make([][]byte, len(strs))
for i, v := range strs {
sorted[i] = countSort(v)
}
hashes := make([][20]byte, len(sorted))
for i, v := range sorted {
hashes[i] = sha1.Sum(v)
}
return hashes
}
// O(N)
func countSort(strs string) []byte {
// TODO: Using golang standard sort for now
bStrs := []byte(strs)
sort.Slice(bStrs, func(i, j int) bool { return bStrs[i] < bStrs[j] })
return bStrs
}