@@ -54,22 +54,160 @@ words[2] = "bell" ,s 开始于 indices[2] = 5 到下一个 '#
54
54
55
55
<!-- 这里可写通用的实现逻辑 -->
56
56
57
+ 题目大意:充分利用重叠的后缀,使有效编码尽可能短
58
+
57
59
<!-- tabs:start -->
58
60
59
61
### ** Python3**
60
62
61
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
64
63
65
``` 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
65
92
```
66
93
67
94
### ** Java**
68
95
69
96
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
97
71
98
``` 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
+ ```
72
171
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
+ };
73
211
```
74
212
75
213
### **...**
0 commit comments