Skip to content

Commit 9c132c0

Browse files
authored
feat: add rust solution to lc problem: No.1048 (#1727)
1 parent 89d3dd6 commit 9c132c0

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

solution/1000-1099/1048.Longest String Chain/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,46 @@ public:
184184
};
185185
```
186186
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+
187227
### **Go**
188228

189229
哈希表:

solution/1000-1099/1048.Longest String Chain/README_EN.md

+40
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,46 @@ public:
165165
};
166166
```
167167
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+
168208
### **Go**
169209

170210
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)