Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b419d00

Browse files
feat: add solutions to lc/lcof2 problems
lc No.0820 & lcof2 No.065 Short Encoding of Words
1 parent 56b32fb commit b419d00

File tree

11 files changed

+671
-3
lines changed

11 files changed

+671
-3
lines changed

‎lcof2/剑指 Offer II 065. 最短的单词编码/README.md‎

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,160 @@ words[2] = "bell" ,s 开始于 indices[2] = 5 到下一个 '#&#3
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
题目大意:充分利用重叠的后缀,使有效编码尽可能短
58+
5759
<!-- tabs:start -->
5860

5961
### **Python3**
6062

6163
<!-- 这里可写当前语言的特殊实现逻辑 -->
6264

6365
```python
64-
66+
class Trie:
67+
def __init__(self) -> None:
68+
self.children = [None] * 26
69+
70+
71+
class Solution:
72+
def minimumLengthEncoding(self, words: List[str]) -> int:
73+
root = Trie()
74+
for w in words:
75+
cur = root
76+
for i in range(len(w) - 1, -1, -1):
77+
idx = ord(w[i]) - ord('a')
78+
if cur.children[idx] == None:
79+
cur.children[idx] = Trie()
80+
cur = cur.children[idx]
81+
return self.dfs(root, 1)
82+
83+
def dfs(self, cur: Trie, l: int) -> int:
84+
isLeaf, ans = True, 0
85+
for i in range(26):
86+
if cur.children[i] != None:
87+
isLeaf = False
88+
ans += self.dfs(cur.children[i], l + 1)
89+
if isLeaf:
90+
ans += l
91+
return ans
6592
```
6693

6794
### **Java**
6895

6996
<!-- 这里可写当前语言的特殊实现逻辑 -->
7097

7198
```java
99+
class Trie {
100+
Trie[] children = new Trie[26];
101+
}
102+
103+
class Solution {
104+
public int minimumLengthEncoding(String[] words) {
105+
Trie root = new Trie();
106+
for (String w : words) {
107+
Trie cur = root;
108+
for (int i = w.length() - 1; i >= 0; i--) {
109+
int idx = w.charAt(i) - 'a';
110+
if (cur.children[idx] == null) {
111+
cur.children[idx] = new Trie();
112+
}
113+
cur = cur.children[idx];
114+
}
115+
}
116+
return dfs(root, 1);
117+
}
118+
119+
private int dfs(Trie cur, int l) {
120+
boolean isLeaf = true;
121+
int ans = 0;
122+
for (int i = 0; i < 26; i++) {
123+
if (cur.children[i] != null) {
124+
isLeaf = false;
125+
ans += dfs(cur.children[i], l + 1);
126+
}
127+
}
128+
if (isLeaf) {
129+
ans += l;
130+
}
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **Go**
137+
138+
```go
139+
type trie struct {
140+
children [26]*trie
141+
}
142+
143+
func minimumLengthEncoding(words []string) int {
144+
root := new(trie)
145+
for _, w := range words {
146+
cur := root
147+
for i := len(w) - 1; i >= 0; i-- {
148+
if cur.children[w[i]-'a'] == nil {
149+
cur.children[w[i]-'a'] = new(trie)
150+
}
151+
cur = cur.children[w[i]-'a']
152+
}
153+
}
154+
return dfs(root, 1)
155+
}
156+
157+
func dfs(cur *trie, l int) int {
158+
isLeaf, ans := true, 0
159+
for i := 0; i < 26; i++ {
160+
if cur.children[i] != nil {
161+
isLeaf = false
162+
ans += dfs(cur.children[i], l+1)
163+
}
164+
}
165+
if isLeaf {
166+
ans += l
167+
}
168+
return ans
169+
}
170+
```
72171

172+
### **C++**
173+
174+
```cpp
175+
struct Trie {
176+
Trie* children[26] = { nullptr };
177+
};
178+
179+
class Solution {
180+
public:
181+
int minimumLengthEncoding(vector<string>& words) {
182+
auto root = new Trie();
183+
for (auto& w : words) {
184+
auto cur = root;
185+
for (int i = w.size() - 1; i >= 0; --i) {
186+
if (cur->children[w[i] - 'a'] == nullptr) {
187+
cur->children[w[i] - 'a'] = new Trie();
188+
}
189+
cur = cur->children[w[i] - 'a'];
190+
}
191+
}
192+
return dfs(root, 1);
193+
}
194+
195+
private:
196+
int dfs(Trie* cur, int l) {
197+
bool isLeaf = true;
198+
int ans = 0;
199+
for (int i = 0; i < 26; ++i) {
200+
if (cur->children[i] != nullptr) {
201+
isLeaf = false;
202+
ans += dfs(cur->children[i], l + 1);
203+
}
204+
}
205+
if (isLeaf) {
206+
ans += l;
207+
}
208+
return ans;
209+
}
210+
};
73211
```
74212
75213
### **...**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
struct Trie {
2+
Trie* children[26] = { nullptr };
3+
};
4+
5+
class Solution {
6+
public:
7+
int minimumLengthEncoding(vector<string>& words) {
8+
auto root = new Trie();
9+
for (auto& w : words) {
10+
auto cur = root;
11+
for (int i = w.size() - 1; i >= 0; --i) {
12+
if (cur->children[w[i] - 'a'] == nullptr) {
13+
cur->children[w[i] - 'a'] = new Trie();
14+
}
15+
cur = cur->children[w[i] - 'a'];
16+
}
17+
}
18+
return dfs(root, 1);
19+
}
20+
21+
private:
22+
int dfs(Trie* cur, int l) {
23+
bool isLeaf = true;
24+
int ans = 0;
25+
for (int i = 0; i < 26; ++i) {
26+
if (cur->children[i] != nullptr) {
27+
isLeaf = false;
28+
ans += dfs(cur->children[i], l + 1);
29+
}
30+
}
31+
if (isLeaf) {
32+
ans += l;
33+
}
34+
return ans;
35+
}
36+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
type trie struct {
2+
children [26]*trie
3+
}
4+
5+
func minimumLengthEncoding(words []string) int {
6+
root := new(trie)
7+
for _, w := range words {
8+
cur := root
9+
for i := len(w) - 1; i >= 0; i-- {
10+
if cur.children[w[i]-'a'] == nil {
11+
cur.children[w[i]-'a'] = new(trie)
12+
}
13+
cur = cur.children[w[i]-'a']
14+
}
15+
}
16+
return dfs(root, 1)
17+
}
18+
19+
func dfs(cur *trie, l int) int {
20+
isLeaf, ans := true, 0
21+
for i := 0; i < 26; i++ {
22+
if cur.children[i] != nil {
23+
isLeaf = false
24+
ans += dfs(cur.children[i], l+1)
25+
}
26+
}
27+
if isLeaf {
28+
ans += l
29+
}
30+
return ans
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Trie {
2+
Trie[] children = new Trie[26];
3+
}
4+
5+
class Solution {
6+
public int minimumLengthEncoding(String[] words) {
7+
Trie root = new Trie();
8+
for (String w : words) {
9+
Trie cur = root;
10+
for (int i = w.length() - 1; i >= 0; i--) {
11+
int idx = w.charAt(i) - 'a';
12+
if (cur.children[idx] == null) {
13+
cur.children[idx] = new Trie();
14+
}
15+
cur = cur.children[idx];
16+
}
17+
}
18+
return dfs(root, 1);
19+
}
20+
21+
private int dfs(Trie cur, int l) {
22+
boolean isLeaf = true;
23+
int ans = 0;
24+
for (int i = 0; i < 26; i++) {
25+
if (cur.children[i] != null) {
26+
isLeaf = false;
27+
ans += dfs(cur.children[i], l + 1);
28+
}
29+
}
30+
if (isLeaf) {
31+
ans += l;
32+
}
33+
return ans;
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Trie:
2+
def __init__(self) -> None:
3+
self.children = [None] * 26
4+
5+
6+
class Solution:
7+
def minimumLengthEncoding(self, words: List[str]) -> int:
8+
root = Trie()
9+
for w in words:
10+
cur = root
11+
for i in range(len(w) - 1, -1, -1):
12+
idx = ord(w[i]) - ord('a')
13+
if cur.children[idx] == None:
14+
cur.children[idx] = Trie()
15+
cur = cur.children[idx]
16+
return self.dfs(root, 1)
17+
18+
def dfs(self, cur: Trie, l: int) -> int:
19+
isLeaf, ans = True, 0
20+
for i in range(26):
21+
if cur.children[i] != None:
22+
isLeaf = False
23+
ans += self.dfs(cur.children[i], l + 1)
24+
if isLeaf:
25+
ans += l
26+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /