Skip to content

Commit 8503840

Browse files
committed
一刷820
1 parent af8a0de commit 8503840

7 files changed

Lines changed: 102 additions & 25 deletions

File tree

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5765,12 +5765,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
57655765
|Easy
57665766
|
57675767

5768-
//|{counter:codes}
5769-
//|{leetcode_base_url}/short-encoding-of-words/[820. Short Encoding of Words^]
5770-
//|{source_base_url}/_0820_ShortEncodingOfWords.java[Java]
5771-
//|{doc_base_url}/0820-short-encoding-of-words.adoc[题解]
5772-
//|Medium
5773-
//|
5768+
|{counter:codes}
5769+
|{leetcode_base_url}/short-encoding-of-words/[820. Short Encoding of Words^]
5770+
|{source_base_url}/_0820_ShortEncodingOfWords.java[Java]
5771+
|{doc_base_url}/0820-short-encoding-of-words.adoc[题解]
5772+
|Medium
5773+
|
57745774

57755775
|{counter:codes}
57765776
|{leetcode_base_url}/shortest-distance-to-a-character/[821. Shortest Distance to a Character^]
Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,78 @@
11
[#0820-short-encoding-of-words]
2-
= 820. Short Encoding of Words
2+
= 820. 单词的压缩编码
33

4-
{leetcode}/problems/short-encoding-of-words/[LeetCode - Short Encoding of Words^]
4+
https://leetcode.cn/problems/short-encoding-of-words/[LeetCode - 820. 单词的压缩编码^]
55

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` 组成,且满足:
77

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]` 相等
911
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` 的长度 。
1113

12-
What is the length of the shortest reference string S possible that encodes the given words?
14+
*示例 1:*
1315

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+
....
1524

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+
*提示:*
2234

23-
35+
* `1 \<= words.length \<= 2000`
36+
* `1 \<= words[i].length \<= 7`
37+
* `words[i]` 仅由小写字母组成
2438
25-
*Note:*
2639
2740
28-
. `1 <= words.length <= 2000`.
29-
. `1 <= words[i].length <= 7`.
30-
. Each word has only lowercase letters.
41+
== 思路分析
3142

43+
投机取巧的办法:给单词数组按照长度从大到小排列(长单词不可能为短单词的后缀),然后逐个判断已有助记词是否保护单词再决定是否添加。
3244

45+
image::images/0820-10.gif[{image_attr}]
3346

47+
更好的解法是:将单词反转,将单词添加到前缀树中,最后根据前缀树叶子简单对应的长度来计算结果。
48+
49+
image::images/0820-11.jpg[{image_attr}]
3450

3551
[[src-0820]]
52+
[tabs]
53+
====
54+
一刷::
55+
+
56+
--
3657
[{java_src_attr}]
3758
----
3859
include::{sourcedir}/_0820_ShortEncodingOfWords.java[tag=answer]
3960
----
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+
== 参考资料
4075

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. 单词的压缩编码 - 无需字典树,轻轻一反转,结果就出来^]

docs/images/0820-10.gif

11.1 MB
Loading

docs/images/0820-11.jpg

165 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ include::0817-linked-list-components.adoc[leveloffset=+1]
17321732

17331733
include::0819-most-common-word.adoc[leveloffset=+1]
17341734

1735-
// include::0820-short-encoding-of-words.adoc[leveloffset=+1]
1735+
include::0820-short-encoding-of-words.adoc[leveloffset=+1]
17361736

17371737
include::0821-shortest-distance-to-a-character.adoc[leveloffset=+1]
17381738

logbook/202601.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ endif::[]
10161016
|{doc_base_url}/0821-shortest-distance-to-a-character.adoc[题解]
10171017
|❌ 双指针。分别从前向后和从后向前遍历。
10181018

1019+
|{counter:codes}
1020+
|{leetcode_base_url}/short-encoding-of-words/[820. 单词的压缩编码^]
1021+
|{doc_base_url}/0820-short-encoding-of-words.adoc[题解]
1022+
|✅ 排序。按长度倒序,后根据判断是否存在在决定是否插入。更优解是前缀树。
1023+
10191024

10201025
|===
10211026

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
public class _0820_ShortEncodingOfWords {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2026-07-14 22:10:14
11+
*/
12+
public int minimumLengthEncoding(String[] words) {
13+
// 按照长度从大到小排列,长度相同则按自然顺序排列
14+
// 更长的单词不可能为别的单词的后缀
15+
Arrays.sort(words, Comparator.comparing(String::length, Comparator.reverseOrder())
16+
.thenComparing(Comparator.naturalOrder()));
17+
StringBuilder sb = new StringBuilder();
18+
for (String word : words) {
19+
if (sb.indexOf(word + "#") >= 0) {
20+
continue;
21+
}
22+
sb.append(word).append('#');
23+
}
24+
return sb.length();
25+
}
26+
27+
// end::answer[]
28+
static void main() {
29+
new _0820_ShortEncodingOfWords()
30+
.minimumLengthEncoding(new String[]{"feipyxx", "e"});
31+
// .minimumLengthEncoding(new String[]{"ctxdic","c"});
32+
// .minimumLengthEncoding(new String[]{"time", "me", "bell"});
33+
}
34+
}

0 commit comments

Comments
 (0)