Skip to content

Commit 11e38a6

Browse files
committed
Add solution 0820
1 parent 21fda4f commit 11e38a6

26 files changed

+1005
-573
lines changed

README.md

+406-400
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package leetcode
2+
3+
// 解法一 暴力
4+
func minimumLengthEncoding(words []string) int {
5+
res, m := 0, map[string]bool{}
6+
for _, w := range words {
7+
m[w] = true
8+
}
9+
for w := range m {
10+
for i := 1; i < len(w); i++ {
11+
delete(m, w[i:])
12+
}
13+
}
14+
for w := range m {
15+
res += len(w) + 1
16+
}
17+
return res
18+
}
19+
20+
// 解法二 Trie
21+
type node struct {
22+
value byte
23+
sub []*node
24+
}
25+
26+
func (t *node) has(b byte) (*node, bool) {
27+
if t == nil {
28+
return nil, false
29+
}
30+
for i := range t.sub {
31+
if t.sub[i] != nil && t.sub[i].value == b {
32+
return t.sub[i], true
33+
}
34+
}
35+
return nil, false
36+
}
37+
38+
func (t *node) isLeaf() bool {
39+
if t == nil {
40+
return false
41+
}
42+
return len(t.sub) == 0
43+
}
44+
45+
func (t *node) add(s []byte) {
46+
now := t
47+
for i := len(s) - 1; i > -1; i-- {
48+
if v, ok := now.has(s[i]); ok {
49+
now = v
50+
continue
51+
}
52+
temp := new(node)
53+
temp.value = s[i]
54+
now.sub = append(now.sub, temp)
55+
now = temp
56+
}
57+
}
58+
59+
func (t *node) endNodeOf(s []byte) *node {
60+
now := t
61+
for i := len(s) - 1; i > -1; i-- {
62+
if v, ok := now.has(s[i]); ok {
63+
now = v
64+
continue
65+
}
66+
return nil
67+
}
68+
return now
69+
}
70+
71+
func minimumLengthEncoding1(words []string) int {
72+
res, tree, m := 0, new(node), make(map[string]bool)
73+
for i := range words {
74+
if !m[words[i]] {
75+
tree.add([]byte(words[i]))
76+
m[words[i]] = true
77+
}
78+
}
79+
for s := range m {
80+
if tree.endNodeOf([]byte(s)).isLeaf() {
81+
res += len(s)
82+
res++
83+
}
84+
}
85+
return res
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question820 struct {
9+
para820
10+
ans820
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para820 struct {
16+
words []string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans820 struct {
22+
one int
23+
}
24+
25+
func Test_Problem820(t *testing.T) {
26+
27+
qs := []question820{
28+
29+
{
30+
para820{[]string{"time", "me", "bell"}},
31+
ans820{10},
32+
},
33+
34+
{
35+
para820{[]string{"t"}},
36+
ans820{2},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 820------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans820, q.para820
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, minimumLengthEncoding(p.words))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# [820. Short Encoding of Words](https://leetcode.com/problems/short-encoding-of-words/)
2+
3+
4+
## 题目
5+
6+
**valid encoding** of an array of `words` is any reference string `s` and array of indices `indices` such that:
7+
8+
- `words.length == indices.length`
9+
- The reference string `s` ends with the `'#'` character.
10+
- For each index `indices[i]`, the **substring** of `s` starting from `indices[i]` and up to (but not including) the next `'#'` character is equal to `words[i]`.
11+
12+
Given an array of `words`, return *the **length of the shortest reference string*** `s` *possible of any **valid encoding** of* `words`*.*
13+
14+
**Example 1:**
15+
16+
```
17+
Input: words = ["time", "me", "bell"]
18+
Output: 10
19+
Explanation: A valid encoding would be s = "time#bell#" and indices = [0, 2, 5].
20+
words[0] = "time", the substring of s starting from indices[0] = 0 to the next '#' is underlined in "time#bell#"
21+
words[1] = "me", the substring of s starting from indices[1] = 2 to the next '#' is underlined in "time#bell#"
22+
words[2] = "bell", the substring of s starting from indices[2] = 5 to the next '#' is underlined in "time#bell#"
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: words = ["t"]
29+
Output: 2
30+
Explanation: A valid encoding would be s = "t#" and indices = [0].
31+
```
32+
33+
**Constraints:**
34+
35+
- `1 <= words.length <= 2000`
36+
- `1 <= words[i].length <= 7`
37+
- `words[i]` consists of only lowercase letters.
38+
39+
## 题目大意
40+
41+
单词数组 words 的 有效编码 由任意助记字符串 s 和下标数组 indices 组成,且满足:
42+
43+
- words.length == indices.length
44+
- 助记字符串 s 以 '#' 字符结尾
45+
- 对于每个下标 indices[i] ,s 的一个从 indices[i] 开始、到下一个 '#' 字符结束(但不包括 '#')的 子字符串 恰好与 words[i] 相等
46+
47+
给你一个单词数组 words ,返回成功对 words 进行编码的最小助记字符串 s 的长度 。
48+
49+
## 解题思路
50+
51+
- 暴力解法。先将所有的单词放入字典中。然后针对字典中的每个单词,逐一从字典中删掉自己的子字符串,这样有相同后缀的字符串被删除了,字典中剩下的都是没有共同前缀的。最终的答案是剩下所有单词用 # 号连接之后的总长度。
52+
- Trie 解法。构建 Trie 树,相同的后缀会被放到从根到叶子节点中的某个路径中。最后依次遍历一遍所有单词,如果单词最后一个字母是叶子节点,说明这个单词是要选择的,因为它可能是包含了一些单词后缀的最长单词。累加这个单词的长度并再加 1(# 字符的长度)。最终累加出来的长度即为题目所求的答案。
53+
54+
## 代码
55+
56+
```go
57+
package leetcode
58+
59+
// 解法一 暴力
60+
func minimumLengthEncoding(words []string) int {
61+
res, m := 0, map[string]bool{}
62+
for _, w := range words {
63+
m[w] = true
64+
}
65+
for w := range m {
66+
for i := 1; i < len(w); i++ {
67+
delete(m, w[i:])
68+
}
69+
}
70+
for w := range m {
71+
res += len(w) + 1
72+
}
73+
return res
74+
}
75+
76+
// 解法二 Trie
77+
type node struct {
78+
value byte
79+
sub []*node
80+
}
81+
82+
func (t *node) has(b byte) (*node, bool) {
83+
if t == nil {
84+
return nil, false
85+
}
86+
for i := range t.sub {
87+
if t.sub[i] != nil && t.sub[i].value == b {
88+
return t.sub[i], true
89+
}
90+
}
91+
return nil, false
92+
}
93+
94+
func (t *node) isLeaf() bool {
95+
if t == nil {
96+
return false
97+
}
98+
return len(t.sub) == 0
99+
}
100+
101+
func (t *node) add(s []byte) {
102+
now := t
103+
for i := len(s) - 1; i > -1; i-- {
104+
if v, ok := now.has(s[i]); ok {
105+
now = v
106+
continue
107+
}
108+
temp := new(node)
109+
temp.value = s[i]
110+
now.sub = append(now.sub, temp)
111+
now = temp
112+
}
113+
}
114+
115+
func (t *node) endNodeOf(s []byte) *node {
116+
now := t
117+
for i := len(s) - 1; i > -1; i-- {
118+
if v, ok := now.has(s[i]); ok {
119+
now = v
120+
continue
121+
}
122+
return nil
123+
}
124+
return now
125+
}
126+
127+
func minimumLengthEncoding1(words []string) int {
128+
res, tree, m := 0, new(node), make(map[string]bool)
129+
for i := range words {
130+
if !m[words[i]] {
131+
tree.add([]byte(words[i]))
132+
m[words[i]] = true
133+
}
134+
}
135+
for s := range m {
136+
if tree.endNodeOf([]byte(s)).isLeaf() {
137+
res += len(s)
138+
res++
139+
}
140+
}
141+
return res
142+
}
143+
```

website/content/ChapterFour/0800~0899/0819.Most-Common-Word.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,5 @@ func mostCommonWord(paragraph string, banned []string) string {
9292
----------------------------------------------
9393
<div style="display: flex;justify-content: space-between;align-items: center;">
9494
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0817.Linked-List-Components/">⬅️上一页</a></p>
95-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0821.Shortest-Distance-to-a-Character/">下一页➡️</a></p>
95+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0820.Short-Encoding-of-Words/">下一页➡️</a></p>
9696
</div>

0 commit comments

Comments
 (0)