|
47 | 47 |
|
48 | 48 | <!-- 这里可写通用的实现逻辑 -->
|
49 | 49 |
|
| 50 | +**方法一:前缀树** |
| 51 | + |
| 52 | +将 $words$ 按照长度分组,构造对应长度的前缀树。 |
| 53 | + |
50 | 54 | <!-- tabs:start -->
|
51 | 55 |
|
52 | 56 | ### **Python3**
|
53 | 57 |
|
54 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 59 |
|
56 | 60 | ```python
|
| 61 | +class Trie: |
| 62 | + def __init__(self): |
| 63 | + self.children = [None] * 26 |
| 64 | + self.v = defaultdict(int) |
| 65 | + |
| 66 | + def insert(self, w): |
| 67 | + node = self |
| 68 | + for c in w: |
| 69 | + idx = ord(c) - ord('a') |
| 70 | + if node.children[idx] is None: |
| 71 | + node.children[idx] = Trie() |
| 72 | + node = node.children[idx] |
| 73 | + node.v[(w[-1], len(w))] += 1 |
| 74 | + |
| 75 | + def search(self, w): |
| 76 | + node = self |
| 77 | + res = [] |
| 78 | + for c in w[:-1]: |
| 79 | + idx = ord(c) - ord('a') |
| 80 | + node = node.children[idx] |
| 81 | + res.append(c) |
| 82 | + if node.v[(w[-1], len(w))] == 1: |
| 83 | + break |
| 84 | + n = len(w) - len(res) - 1 |
| 85 | + if n: |
| 86 | + res.append(str(n)) |
| 87 | + res.append(w[-1]) |
| 88 | + t = ''.join(res) |
| 89 | + return t if len(t) < len(w) else w |
| 90 | + |
| 91 | + |
| 92 | +class Solution: |
| 93 | + def wordsAbbreviation(self, words: List[str]) -> List[str]: |
| 94 | + trie = Trie() |
| 95 | + for w in words: |
| 96 | + trie.insert(w) |
| 97 | + return [trie.search(w) for w in words] |
| 98 | +``` |
| 99 | + |
| 100 | +```python |
| 101 | +class Trie: |
| 102 | + def __init__(self): |
| 103 | + self.children = [None] * 26 |
| 104 | + self.v = Counter() |
| 105 | + |
| 106 | + def insert(self, w): |
| 107 | + node = self |
| 108 | + for c in w: |
| 109 | + idx = ord(c) - ord('a') |
| 110 | + if node.children[idx] is None: |
| 111 | + node.children[idx] = Trie() |
| 112 | + node = node.children[idx] |
| 113 | + node.v[w[-1]] += 1 |
| 114 | + |
| 115 | + def search(self, w): |
| 116 | + node = self |
| 117 | + res = [] |
| 118 | + for c in w[:-1]: |
| 119 | + idx = ord(c) - ord('a') |
| 120 | + node = node.children[idx] |
| 121 | + res.append(c) |
| 122 | + if node.v[w[-1]] == 1: |
| 123 | + break |
| 124 | + n = len(w) - len(res) - 1 |
| 125 | + if n: |
| 126 | + res.append(str(n)) |
| 127 | + res.append(w[-1]) |
| 128 | + t = ''.join(res) |
| 129 | + return t if len(t) < len(w) else w |
57 | 130 |
|
| 131 | +class Solution: |
| 132 | + def wordsAbbreviation(self, words: List[str]) -> List[str]: |
| 133 | + trees = {} |
| 134 | + for w in words: |
| 135 | + if len(w) not in trees: |
| 136 | + trees[len(w)] = Trie() |
| 137 | + for w in words: |
| 138 | + trees[len(w)].insert(w) |
| 139 | + return [trees[len(w)].search(w) for w in words] |
58 | 140 | ```
|
59 | 141 |
|
60 | 142 | ### **Java**
|
61 | 143 |
|
62 | 144 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 145 |
|
64 | 146 | ```java
|
| 147 | +class Trie { |
| 148 | + Trie[] children = new Trie[26]; |
| 149 | + int[] v = new int[26]; |
| 150 | + |
| 151 | + void insert(String w) { |
| 152 | + Trie node = this; |
| 153 | + int t = w.charAt(w.length() - 1) - 'a'; |
| 154 | + for (char c : w.toCharArray()) { |
| 155 | + c -= 'a'; |
| 156 | + if (node.children[c] == null) { |
| 157 | + node.children[c] = new Trie(); |
| 158 | + } |
| 159 | + node = node.children[c]; |
| 160 | + node.v[t]++; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + String search(String w) { |
| 165 | + Trie node = this; |
| 166 | + StringBuilder res = new StringBuilder(); |
| 167 | + int t = w.charAt(w.length() - 1) - 'a'; |
| 168 | + for (int i = 0; i < w.length() - 1; ++i) { |
| 169 | + char c = w.charAt(i); |
| 170 | + node = node.children[c - 'a']; |
| 171 | + res.append(c); |
| 172 | + if (node.v[t] == 1) { |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
| 176 | + int n = w.length() - res.length() - 1; |
| 177 | + if (n > 0) { |
| 178 | + res.append(n); |
| 179 | + } |
| 180 | + res.append(w.charAt(w.length() - 1)); |
| 181 | + return res.length() < w.length() ? res.toString() : w; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +class Solution { |
| 186 | + public List<String> wordsAbbreviation(List<String> words) { |
| 187 | + Map<Integer, Trie> trees = new HashMap<>(); |
| 188 | + for (String w : words) { |
| 189 | + if (!trees.containsKey(w.length())) { |
| 190 | + trees.put(w.length(), new Trie()); |
| 191 | + } |
| 192 | + } |
| 193 | + for (String w : words) { |
| 194 | + trees.get(w.length()).insert(w); |
| 195 | + } |
| 196 | + List<String> ans = new ArrayList<>(); |
| 197 | + for (String w : words) { |
| 198 | + ans.add(trees.get(w.length()).search(w)); |
| 199 | + } |
| 200 | + return ans; |
| 201 | + } |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +### **Go** |
| 206 | + |
| 207 | +```go |
| 208 | +type Trie struct { |
| 209 | + children [26]*Trie |
| 210 | + v [26]int |
| 211 | +} |
| 212 | + |
| 213 | +func newTrie() *Trie { |
| 214 | + return &Trie{} |
| 215 | +} |
| 216 | +func (this *Trie) insert(w string) { |
| 217 | + node := this |
| 218 | + t := w[len(w)-1] - 'a' |
| 219 | + for _, c := range w { |
| 220 | + c -= 'a' |
| 221 | + if node.children[c] == nil { |
| 222 | + node.children[c] = newTrie() |
| 223 | + } |
| 224 | + node = node.children[c] |
| 225 | + node.v[t]++ |
| 226 | + } |
| 227 | +} |
| 228 | +func (this *Trie) search(w string) string { |
| 229 | + node := this |
| 230 | + t := w[len(w)-1] - 'a' |
| 231 | + res := &strings.Builder{} |
| 232 | + for _, c := range w[:len(w)-1] { |
| 233 | + res.WriteRune(c) |
| 234 | + c -= 'a' |
| 235 | + node = node.children[c] |
| 236 | + if node.v[t] == 1 { |
| 237 | + break |
| 238 | + } |
| 239 | + } |
| 240 | + n := len(w) - res.Len() - 1 |
| 241 | + if n > 0 { |
| 242 | + res.WriteString(strconv.Itoa(n)) |
| 243 | + } |
| 244 | + res.WriteByte(w[len(w)-1]) |
| 245 | + if res.Len() < len(w) { |
| 246 | + return res.String() |
| 247 | + } |
| 248 | + return w |
| 249 | +} |
65 | 250 |
|
| 251 | +func wordsAbbreviation(words []string) []string { |
| 252 | + trees := map[int]*Trie{} |
| 253 | + for _, w := range words { |
| 254 | + if _, ok := trees[len(w)]; !ok { |
| 255 | + trees[len(w)] = newTrie() |
| 256 | + } |
| 257 | + } |
| 258 | + for _, w := range words { |
| 259 | + trees[len(w)].insert(w) |
| 260 | + } |
| 261 | + ans := []string{} |
| 262 | + for _, w := range words { |
| 263 | + ans = append(ans, trees[len(w)].search(w)) |
| 264 | + } |
| 265 | + return ans |
| 266 | +} |
66 | 267 | ```
|
67 | 268 |
|
68 | 269 | ### **...**
|
|
0 commit comments