|
1 | 1 | [#0820-short-encoding-of-words] |
2 | | -= 820. Short Encoding of Words |
| 2 | += 820. 单词的压缩编码 |
3 | 3 |
|
4 | | -{leetcode}/problems/short-encoding-of-words/[LeetCode - Short Encoding of Words^] |
| 4 | +https://leetcode.cn/problems/short-encoding-of-words/[LeetCode - 820. 单词的压缩编码^] |
5 | 5 |
|
6 | | -Given a list of words, we may encode it by writing a reference string `S` and a list of indexes `A`. |
| 6 | +单词数组 `words` 的 *有效编码* 由任意助记字符串 `s` 和下标数组 `indices` 组成,且满足: |
7 | 7 |
|
8 | | -For example, if the list of words is `["time", "me", "bell"]`, we can write it as `S = "time#bell#"` and `indexes = [0, 2, 5]`. |
| 8 | +* `words.length == indices.length` |
| 9 | +* 助记字符串 `s` 以 `#` 字符结尾 |
| 10 | +* 对于每个下标 `indices[i]` ,`s` 的一个从 `indices[i]` 开始、到下一个 `#` 字符结束(但不包括 `#`)的 *子字符串* 恰好与 `words[i]` 相等 |
9 | 11 |
|
10 | | -Then for each index, we will recover the word by reading from the reference string from that index until we reach a `"#"` character. |
| 12 | +给你一个单词数组 `words` ,返回成功对 `words` 进行编码的最小助记字符串 `s` 的长度 。 |
11 | 13 |
|
12 | | -What is the length of the shortest reference string S possible that encodes the given words? |
| 14 | +*示例 1:* |
13 | 15 |
|
14 | | -*Example:* |
| 16 | +.... |
| 17 | +输入:words = ["time", "me", "bell"] |
| 18 | +输出:10 |
| 19 | +解释:一组有效编码为 s = "time#bell#" 和 indices = [0, 2, 5] 。 |
| 20 | +words[0] = "time" ,s 开始于 indices[0] = 0 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#" |
| 21 | +words[1] = "me" ,s 开始于 indices[1] = 2 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#" |
| 22 | +words[2] = "bell" ,s 开始于 indices[2] = 5 到下一个 '#' 结束的子字符串,如加粗部分所示 "time#bell#" |
| 23 | +.... |
15 | 24 |
|
16 | | -[subs="verbatim,quotes,macros"] |
17 | | ----- |
18 | | -*Input:* words = `["time", "me", "bell"]` |
19 | | -*Output:* 10 |
20 | | -*Explanation:* S = `"time#bell#" and indexes = [0, 2, 5`]. |
21 | | ----- |
| 25 | +*示例 2:* |
| 26 | + |
| 27 | +.... |
| 28 | +输入:words = ["t"] |
| 29 | +输出:2 |
| 30 | +解释:一组有效编码为 s = "t#" 和 indices = [0] 。 |
| 31 | +.... |
| 32 | + |
| 33 | +*提示:* |
22 | 34 |
|
23 | | - |
| 35 | +* `1 \<= words.length \<= 2000` |
| 36 | +* `1 \<= words[i].length \<= 7` |
| 37 | +* `words[i]` 仅由小写字母组成 |
24 | 38 |
|
25 | | -*Note:* |
26 | 39 |
|
27 | 40 |
|
28 | | -. `1 <= words.length <= 2000`. |
29 | | -. `1 <= words[i].length <= 7`. |
30 | | -. Each word has only lowercase letters. |
| 41 | +== 思路分析 |
31 | 42 |
|
| 43 | +投机取巧的办法:给单词数组按照长度从大到小排列(长单词不可能为短单词的后缀),然后逐个判断已有助记词是否保护单词再决定是否添加。 |
32 | 44 |
|
| 45 | +image::images/0820-10.gif[{image_attr}] |
33 | 46 |
|
| 47 | +更好的解法是:将单词反转,将单词添加到前缀树中,最后根据前缀树叶子简单对应的长度来计算结果。 |
| 48 | + |
| 49 | +image::images/0820-11.jpg[{image_attr}] |
34 | 50 |
|
35 | 51 | [[src-0820]] |
| 52 | +[tabs] |
| 53 | +==== |
| 54 | +一刷:: |
| 55 | ++ |
| 56 | +-- |
36 | 57 | [{java_src_attr}] |
37 | 58 | ---- |
38 | 59 | include::{sourcedir}/_0820_ShortEncodingOfWords.java[tag=answer] |
39 | 60 | ---- |
| 61 | +-- |
| 62 | +
|
| 63 | +// 二刷:: |
| 64 | +// + |
| 65 | +// -- |
| 66 | +// [{java_src_attr}] |
| 67 | +// ---- |
| 68 | +// include::{sourcedir}/_0820_ShortEncodingOfWords_2.java[tag=answer] |
| 69 | +// ---- |
| 70 | +// -- |
| 71 | +==== |
| 72 | + |
| 73 | + |
| 74 | +== 参考资料 |
40 | 75 |
|
| 76 | +. https://leetcode.cn/problems/short-encoding-of-words/solutions/173709/dan-ci-de-ya-suo-bian-ma-by-leetcode-solution/[820. 单词的压缩编码 - 官方题解^] |
| 77 | +. https://leetcode.cn/problems/short-encoding-of-words/solutions/174404/99-java-trie-tu-xie-gong-lue-bao-jiao-bao-hui-by-s/[820. 单词的压缩编码 - 99% Trie 吐血攻略,包教包会^] |
| 78 | +. https://leetcode.cn/problems/short-encoding-of-words/solutions/174362/wu-xu-zi-dian-shu-qing-qing-yi-fan-zhuan-jie-guo-j/[820. 单词的压缩编码 - 无需字典树,轻轻一反转,结果就出来^] |
0 commit comments