Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rust solution to lc problem: No.1048 #1727

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions solution/1000-1099/1048.Longest String Chain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,46 @@ public:
};
```

### **Rust**

```rust
use std::collections::HashMap;

impl Solution {
#[allow(dead_code)]
pub fn longest_str_chain(words: Vec<String>) -> i32 {
let mut words = words;
let mut ret = 0;
let mut map: HashMap<String, i32> = HashMap::new();

// Sort the words vector first
words.sort_by(|lhs, rhs| {
lhs.len().cmp(&rhs.len())
});

// Begin the "dp" process
for w in words.iter() {
let n = w.len();
let mut x = 1;

for i in 0..n {
let s = w[..i].to_string() + &w[i + 1..];
let v = map
.entry(s.clone())
.or_default();
x = std::cmp::max(x, *v + 1);
}

map.insert(w.clone(), x);

ret = std::cmp::max(ret, x);
}

ret
}
}
```

### **Go**

哈希表:
Expand Down
40 changes: 40 additions & 0 deletions solution/1000-1099/1048.Longest String Chain/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,46 @@ public:
};
```

### **Rust**

```rust
use std::collections::HashMap;

impl Solution {
#[allow(dead_code)]
pub fn longest_str_chain(words: Vec<String>) -> i32 {
let mut words = words;
let mut ret = 0;
let mut map: HashMap<String, i32> = HashMap::new();

// Sort the words vector first
words.sort_by(|lhs, rhs| {
lhs.len().cmp(&rhs.len())
});

// Begin the "dp" process
for w in words.iter() {
let n = w.len();
let mut x = 1;

for i in 0..n {
let s = w[..i].to_string() + &w[i + 1..];
let v = map
.entry(s.clone())
.or_default();
x = std::cmp::max(x, *v + 1);
}

map.insert(w.clone(), x);

ret = std::cmp::max(ret, x);
}

ret
}
}
```

### **Go**

```go
Expand Down
35 changes: 35 additions & 0 deletions solution/1000-1099/1048.Longest String Chain/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::HashMap;

impl Solution {
#[allow(dead_code)]
pub fn longest_str_chain(words: Vec<String>) -> i32 {
let mut words = words;
let mut ret = 0;
let mut map: HashMap<String, i32> = HashMap::new();

// Sort the words vector first
words.sort_by(|lhs, rhs| {
lhs.len().cmp(&rhs.len())
});

// Begin the "dp" process
for w in words.iter() {
let n = w.len();
let mut x = 1;

for i in 0..n {
let s = w[..i].to_string() + &w[i + 1..];
let v = map
.entry(s.clone())
.or_default();
x = std::cmp::max(x, *v + 1);
}

map.insert(w.clone(), x);

ret = std::cmp::max(ret, x);
}

ret
}
}