From f774baa2ffacc9fbb8cb6c56121d8672db438b4e Mon Sep 17 00:00:00 2001 From: wavty Date: Mon, 4 Sep 2023 00:43:15 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.1071 No.1071.Greatest Common Divisor of Strings --- .../README.md | 21 +++++++++++++++++++ .../README_EN.md | 21 +++++++++++++++++++ .../Solution.rs | 16 ++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs diff --git a/solution/1000-1099/1071.Greatest Common Divisor of Strings/README.md b/solution/1000-1099/1071.Greatest Common Divisor of Strings/README.md index ed9dfd498f297..caa79198820e9 100644 --- a/solution/1000-1099/1071.Greatest Common Divisor of Strings/README.md +++ b/solution/1000-1099/1071.Greatest Common Divisor of Strings/README.md @@ -129,6 +129,27 @@ func gcd(a, b int) int { } ``` +### **Rust** + +```rust +impl Solution { + pub fn gcd_of_strings(str1: String, str2: String) -> String { + if str1.clone() + &str2 != str2.clone() + &str1 { + return String::from(""); + } + fn gcd(a: usize, b: usize) -> usize { + if b == 0 { + return a; + } + gcd(b, a % b) + } + + let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len())); + str1[..gcd(m, n)].to_string() + } +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1071.Greatest Common Divisor of Strings/README_EN.md b/solution/1000-1099/1071.Greatest Common Divisor of Strings/README_EN.md index 39fbf2ee107d9..7b7e4b54ce401 100644 --- a/solution/1000-1099/1071.Greatest Common Divisor of Strings/README_EN.md +++ b/solution/1000-1099/1071.Greatest Common Divisor of Strings/README_EN.md @@ -119,6 +119,27 @@ func gcd(a, b int) int { } ``` +### **Rust** + +```rust +impl Solution { + pub fn gcd_of_strings(str1: String, str2: String) -> String { + if str1.clone() + &str2 != str2.clone() + &str1 { + return String::from(""); + } + fn gcd(a: usize, b: usize) -> usize { + if b == 0 { + return a; + } + gcd(b, a % b) + } + + let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len())); + str1[..gcd(m, n)].to_string() + } +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs b/solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs new file mode 100644 index 0000000000000..949fb85d4389d --- /dev/null +++ b/solution/1000-1099/1071.Greatest Common Divisor of Strings/Solution.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn gcd_of_strings(str1: String, str2: String) -> String { + if str1.clone() + &str2 != str2.clone() + &str1 { + return String::from(""); + } + fn gcd(a: usize, b: usize) -> usize { + if b == 0 { + return a; + } + gcd(b, a % b) + } + + let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len())); + str1[..gcd(m, n)].to_string() + } +}