-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram.go
51 lines (43 loc) · 1.06 KB
/
anagram.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
package main
import (
"fmt"
"sort"
"strings"
)
type targetcnts struct {
letter string
occurs int
}
func main() {
target := "listtenerse"
candidates := []string{"enlists", "google", "inlets", "banana", "inlett"}
i := 0
var targetcnt []targetcnts
for _, r := range target {
c := strings.ToLower(string(r))
targetcnt = append(targetcnt, targetcnts{c, 1})
}
sort.Slice(targetcnt, func(i, j int) bool { return targetcnt[i].letter < targetcnt[j].letter })
for m := 0; m < len(targetcnt)-1; m++ {
if targetcnt[m].letter == targetcnt[m+1].letter {
targetcnt[m].occurs++
targetcnt[m+1].letter = ""
targetcnt[m+1].occurs = 0
sort.Slice(targetcnt, func(i, j int) bool { return targetcnt[i].letter < targetcnt[j].letter })
}
}
fmt.Printf("targetcnt = %v\n", targetcnt)
for x := range candidates {
for _, y := range candidates[x] {
if strings.ContainsRune(target, y) {
i = 0
} else {
i = 1
break
}
}
if i == 0 && len(target) == len(candidates[x]) {
fmt.Printf("%v is an anagram of %v\n", candidates[x], target)
}
}
}