-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
55 lines (51 loc) · 975 Bytes
/
Solution.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
type Trie struct {
children [26]*Trie
cnt int
}
func (t *Trie) insert(w string) {
node := t
for _, c := range w {
idx := c - 'a'
if node.children[idx] == nil {
node.children[idx] = &Trie{}
}
node = node.children[idx]
node.cnt++
}
}
func (t *Trie) search(w string) int {
node := t
ans := 0
for _, c := range w {
ans++
idx := c - 'a'
node = node.children[idx]
if node.cnt == 1 {
return ans
}
}
return len(w)
}
func wordsAbbreviation(words []string) (ans []string) {
tries := make(map[[2]int]*Trie)
for _, w := range words {
key := [2]int{len(w), int(w[len(w)-1] - 'a')}
_, exists := tries[key]
if !exists {
tries[key] = &Trie{}
}
tries[key].insert(w)
}
for _, w := range words {
m := len(w)
key := [2]int{m, int(w[m-1] - 'a')}
cnt := tries[key].search(w)
if cnt+2 >= m {
ans = append(ans, w)
} else {
abbr := w[:cnt] + fmt.Sprintf("%d", m-cnt-1) + w[m-1:]
ans = append(ans, abbr)
}
}
return
}