-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (148 loc) · 3.86 KB
/
main.go
File metadata and controls
171 lines (148 loc) · 3.86 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Source: https://leetcode.com/problems/word-abbreviation/
// Title: Word Abbreviation
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an array of **distinct** strings `words`, return the minimal possible **abbreviations** for every word.
//
// The following are the rules for a string abbreviation:
//
// - The **initial** abbreviation for each word is: the first character, then the number of characters in between, followed by the last character.
// - If more than one word shares the **same** abbreviation, then perform the following operation:
//
// - **Increase** the prefix (characters in the first part) of each of their abbreviations by `1`.
//
// - For example, say you start with the words `["abcdef","abndef"]` both initially abbreviated as `"a4f"`. Then, a sequence of operations would be `["a4f","a4f"]` -> `["ab3f","ab3f"]` -> `["abc2f","abn2f"]`.
//
// - This operation is repeated until every abbreviation is **unique**.
//
// - At the end, if an abbreviation did not make a word shorter, then keep it as the original word.
//
// **Example 1:**
//
// ```
// Input: words = ["like","god","internal","me","internet","interval","intension","face","intrusion"]
// Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]
// ```
//
// **Example 2:**
//
// ```
// Input: words = ["aa","aaa"]
// Output: ["aa","aaa"]
// ```
//
// **Constraints:**
//
// - `1 <= words.length <= 400`
// - `2 <= words[i].length <= 400`
// - `words[i]` consists of lowercase English letters.
// - All the strings of `words` are **unique**.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"strconv"
)
// DFS + Trie
func wordsAbbreviation(words []string) []string {
n := len(words)
abbrs := make([]string, n)
type trieKey struct {
start byte
end byte
size int
}
type trieNode struct {
children map[byte]*trieNode
ids []int
}
newTrieNode := func() *trieNode {
return &trieNode{
children: make(map[byte]*trieNode),
}
}
tries := make(map[trieKey]*trieNode)
// Create trie
for id, word := range words {
l := len(word)
if l < 2 {
abbrs[id] = word
continue
}
// Root
key := trieKey{word[0], word[l-1], l}
if _, ok := tries[key]; !ok {
tries[key] = newTrieNode()
}
node := tries[key]
node.ids = append(node.ids, id)
// Trie
for i := 1; i < l-1; i++ {
ch := word[i]
if _, ok := node.children[ch]; !ok {
node.children[ch] = newTrieNode()
}
node = node.children[ch]
node.ids = append(node.ids, id)
}
}
// DFS
var dfs func(key trieKey, node *trieNode, depth int)
dfs = func(key trieKey, node *trieNode, depth int) {
if depth == key.size {
return
}
if len(node.ids) == 1 {
id := node.ids[0]
word := words[id]
l := len(word)
abbr := word[:depth] + strconv.Itoa(l-1-depth) + string(word[l-1])
if len(abbr) >= l {
abbr = word
}
abbrs[id] = abbr
return
}
for _, child := range node.children {
dfs(key, child, depth+1)
}
}
for key, trie := range tries {
dfs(key, trie, 1)
}
return abbrs
}
// Greedy, Two Loop
func wordsAbbreviation2(words []string) []string {
n := len(words)
abbrs := make([]string, n)
makeAbbr := func(word string, i int) string {
l := len(word)
if l-i <= 3 {
return word
}
return word[:i+1] + strconv.Itoa(l-i-2) + word[l-1:]
}
for i := range n {
abbrs[i] = makeAbbr(words[i], 0)
}
for i := range n {
for l := 1; ; l++ {
dupes := map[int]bool{}
for j := i + 1; j < n; j++ {
if abbrs[i] == abbrs[j] {
dupes[j] = true
}
}
if len(dupes) == 0 {
break
}
dupes[i] = true
for j := range dupes {
abbrs[j] = makeAbbr(words[j], l)
}
}
}
return abbrs
}