-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathanagram_together.go
94 lines (74 loc) · 1.75 KB
/
anagram_together.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"sort"
)
func main() {
strs := []string{"art", "tap", "rat", "pat", "tar", "arm"}
output := groupAnagrams(strs)
fmt.Println(output)
strs = []string{""}
output = groupAnagrams(strs)
fmt.Println(output)
strs = []string{"a"}
output = groupAnagrams(strs)
fmt.Println(output)
}
type sortRune []rune
func (s sortRune) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortRune) Less(i, j int) bool {
return s[i] < s[j]
}
func (s sortRune) Len() int {
return len(s)
}
func groupAnagrams(strs []string) [][]string {
anagramMap := make(map[string][]int)
var anagrams [][]string
trie := &trie{root: &trieNode{}}
lenStrs := len(strs)
var strsDup []string
for i := 0; i < lenStrs; i++ {
runeCurrent := []rune(strs[i])
sort.Sort(sortRune(runeCurrent))
strsDup = append(strsDup, string(runeCurrent))
}
for i := 0; i < lenStrs; i++ {
anagramMap = trie.insert(strsDup[i], i, anagramMap)
}
for _, value := range anagramMap {
var combinedTemp []string
for i := 0; i < len(value); i++ {
combinedTemp = append(combinedTemp, strs[value[i]])
}
anagrams = append(anagrams, combinedTemp)
}
return anagrams
}
type trieNode struct {
isWord bool
childrens [26]*trieNode
}
type trie struct {
root *trieNode
}
func (t *trie) insert(input string, wordIndex int, anagramMap map[string][]int) map[string][]int {
inputLen := len(input)
current := t.root
for i := 0; i < inputLen; i++ {
index := input[i] - 'a'
if current.childrens[index] == nil {
current.childrens[index] = &trieNode{}
}
current = current.childrens[index]
}
current.isWord = true
if anagramMap[input] == nil {
anagramMap[input] = []int{wordIndex}
} else {
anagramMap[input] = append(anagramMap[input], wordIndex)
}
return anagramMap
}