-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagrams.go
58 lines (51 loc) · 1.12 KB
/
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
58
// Group Anagrams
// https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/778/
//
package main
import (
"log"
)
func main() {
log.Println(groupAnagrams([]string{"aaan", "naaa", "naa", "ana", "aan", "aana"}))
}
type AnaGroup struct {
Checksum []uint8
Accum []string
}
func groupAnagrams(strs []string) [][]string {
anaGroups := []AnaGroup{}
for _, wd := range strs {
wdAnd := anagramChecksum(wd)
found := false
for i := 0; i < len(anaGroups); i++ {
if anagramsEqual(anaGroups[i].Checksum, wdAnd) {
anaGroups[i].Accum = append(anaGroups[i].Accum, wd)
found = true
}
}
if !found {
anaGroups = append(anaGroups, AnaGroup{Checksum: wdAnd, Accum: []string{wd}})
}
}
//compose output
ans := make([][]string, len(anaGroups))
for k, v := range anaGroups {
ans[k] = v.Accum
}
return ans
}
func anagramChecksum(wd string) []uint8 {
ana := make([]uint8, 26)
for i := 0; i < len(wd); i++ {
ana[rune(wd[i])-97]++
}
return ana
}
func anagramsEqual(a, b []uint8) bool {
for i := 0; i < 26; i++ {
if a[i] != b[i] {
return false
}
}
return true
}