From 8a65c00e80c58260036e04fe81f1d2173af1762e Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Sat, 30 Sep 2023 02:34:48 -0400 Subject: [PATCH] feat: add rust solution to lc problem: No.1048 --- .../1048.Longest String Chain/README.md | 40 +++++++++++++++++++ .../1048.Longest String Chain/README_EN.md | 40 +++++++++++++++++++ .../1048.Longest String Chain/Solution.rs | 35 ++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 solution/1000-1099/1048.Longest String Chain/Solution.rs diff --git a/solution/1000-1099/1048.Longest String Chain/README.md b/solution/1000-1099/1048.Longest String Chain/README.md index 9d05f5c59b09c..01b9f0b270065 100644 --- a/solution/1000-1099/1048.Longest String Chain/README.md +++ b/solution/1000-1099/1048.Longest String Chain/README.md @@ -184,6 +184,46 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + #[allow(dead_code)] + pub fn longest_str_chain(words: Vec) -> i32 { + let mut words = words; + let mut ret = 0; + let mut map: HashMap = 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** 哈希表: diff --git a/solution/1000-1099/1048.Longest String Chain/README_EN.md b/solution/1000-1099/1048.Longest String Chain/README_EN.md index 5bbc04d8d66d8..8f263f7c61580 100644 --- a/solution/1000-1099/1048.Longest String Chain/README_EN.md +++ b/solution/1000-1099/1048.Longest String Chain/README_EN.md @@ -165,6 +165,46 @@ public: }; ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + #[allow(dead_code)] + pub fn longest_str_chain(words: Vec) -> i32 { + let mut words = words; + let mut ret = 0; + let mut map: HashMap = 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 diff --git a/solution/1000-1099/1048.Longest String Chain/Solution.rs b/solution/1000-1099/1048.Longest String Chain/Solution.rs new file mode 100644 index 0000000000000..3b08738682b90 --- /dev/null +++ b/solution/1000-1099/1048.Longest String Chain/Solution.rs @@ -0,0 +1,35 @@ +use std::collections::HashMap; + +impl Solution { + #[allow(dead_code)] + pub fn longest_str_chain(words: Vec) -> i32 { + let mut words = words; + let mut ret = 0; + let mut map: HashMap = 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 + } +} \ No newline at end of file