File tree 3 files changed +115
-0
lines changed
solution/1000-1099/1048.Longest String Chain
3 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,46 @@ public:
184
184
};
185
185
```
186
186
187
+ ### **Rust**
188
+
189
+ ```rust
190
+ use std::collections::HashMap;
191
+
192
+ impl Solution {
193
+ #[allow(dead_code)]
194
+ pub fn longest_str_chain(words: Vec<String>) -> i32 {
195
+ let mut words = words;
196
+ let mut ret = 0;
197
+ let mut map: HashMap<String, i32> = HashMap::new();
198
+
199
+ // Sort the words vector first
200
+ words.sort_by(|lhs, rhs| {
201
+ lhs.len().cmp(&rhs.len())
202
+ });
203
+
204
+ // Begin the "dp" process
205
+ for w in words.iter() {
206
+ let n = w.len();
207
+ let mut x = 1;
208
+
209
+ for i in 0..n {
210
+ let s = w[..i].to_string() + &w[i + 1..];
211
+ let v = map
212
+ .entry(s.clone())
213
+ .or_default();
214
+ x = std::cmp::max(x, *v + 1);
215
+ }
216
+
217
+ map.insert(w.clone(), x);
218
+
219
+ ret = std::cmp::max(ret, x);
220
+ }
221
+
222
+ ret
223
+ }
224
+ }
225
+ ```
226
+
187
227
### ** Go**
188
228
189
229
哈希表:
Original file line number Diff line number Diff line change @@ -165,6 +165,46 @@ public:
165
165
};
166
166
```
167
167
168
+ ### **Rust**
169
+
170
+ ```rust
171
+ use std::collections::HashMap;
172
+
173
+ impl Solution {
174
+ #[allow(dead_code)]
175
+ pub fn longest_str_chain(words: Vec<String>) -> i32 {
176
+ let mut words = words;
177
+ let mut ret = 0;
178
+ let mut map: HashMap<String, i32> = HashMap::new();
179
+
180
+ // Sort the words vector first
181
+ words.sort_by(|lhs, rhs| {
182
+ lhs.len().cmp(&rhs.len())
183
+ });
184
+
185
+ // Begin the "dp" process
186
+ for w in words.iter() {
187
+ let n = w.len();
188
+ let mut x = 1;
189
+
190
+ for i in 0..n {
191
+ let s = w[..i].to_string() + &w[i + 1..];
192
+ let v = map
193
+ .entry(s.clone())
194
+ .or_default();
195
+ x = std::cmp::max(x, *v + 1);
196
+ }
197
+
198
+ map.insert(w.clone(), x);
199
+
200
+ ret = std::cmp::max(ret, x);
201
+ }
202
+
203
+ ret
204
+ }
205
+ }
206
+ ```
207
+
168
208
### ** Go**
169
209
170
210
``` go
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
3
+ impl Solution {
4
+ #[ allow( dead_code) ]
5
+ pub fn longest_str_chain ( words : Vec < String > ) -> i32 {
6
+ let mut words = words;
7
+ let mut ret = 0 ;
8
+ let mut map: HashMap < String , i32 > = HashMap :: new ( ) ;
9
+
10
+ // Sort the words vector first
11
+ words. sort_by ( |lhs, rhs| {
12
+ lhs. len ( ) . cmp ( & rhs. len ( ) )
13
+ } ) ;
14
+
15
+ // Begin the "dp" process
16
+ for w in words. iter ( ) {
17
+ let n = w. len ( ) ;
18
+ let mut x = 1 ;
19
+
20
+ for i in 0 ..n {
21
+ let s = w[ ..i] . to_string ( ) + & w[ i + 1 ..] ;
22
+ let v = map
23
+ . entry ( s. clone ( ) )
24
+ . or_default ( ) ;
25
+ x = std:: cmp:: max ( x, * v + 1 ) ;
26
+ }
27
+
28
+ map. insert ( w. clone ( ) , x) ;
29
+
30
+ ret = std:: cmp:: max ( ret, x) ;
31
+ }
32
+
33
+ ret
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments