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() + } +}