forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0820.go
More file actions
34 lines (29 loc) · 657 Bytes
/
0820.go
File metadata and controls
34 lines (29 loc) · 657 Bytes
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
var nodes map[*Trie]int
func minimumLengthEncoding(words []string) int {
root := &Trie{next: make(map[byte]*Trie)}
nodes = make(map[*Trie]int)
for i, word := range words {
root.Insert(word, i)
}
res := 0
for node, i := range nodes {
if len(node.next) == 0 {
res += len(words[i]) + 1
}
}
return res
}
type Trie struct {
next map[byte]*Trie
}
func (this *Trie) Insert(word string, id int) {
cur := this
for i := len(word) - 1; i >= 0; i-- {
c := word[i]
if cur.next[c] == nil {
cur.next[c] = &Trie{next: make(map[byte]*Trie)}
}
cur = cur.next[c]
}
nodes[cur] = id
}