Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff708ad

Browse files
authored
feat: add rust solution to lc problem: No.1071 (#1570)
No.1071.Greatest Common Divisor of Strings
1 parent 1187dc8 commit ff708ad

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

‎solution/1000-1099/1071.Greatest Common Divisor of Strings/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,27 @@ func gcd(a, b int) int {
129129
}
130130
```
131131

132+
### **Rust**
133+
134+
```rust
135+
impl Solution {
136+
pub fn gcd_of_strings(str1: String, str2: String) -> String {
137+
if str1.clone() + &str2 != str2.clone() + &str1 {
138+
return String::from("");
139+
}
140+
fn gcd(a: usize, b: usize) -> usize {
141+
if b == 0 {
142+
return a;
143+
}
144+
gcd(b, a % b)
145+
}
146+
147+
let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len()));
148+
str1[..gcd(m, n)].to_string()
149+
}
150+
}
151+
```
152+
132153
### **...**
133154

134155
```

‎solution/1000-1099/1071.Greatest Common Divisor of Strings/README_EN.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ func gcd(a, b int) int {
119119
}
120120
```
121121

122+
### **Rust**
123+
124+
```rust
125+
impl Solution {
126+
pub fn gcd_of_strings(str1: String, str2: String) -> String {
127+
if str1.clone() + &str2 != str2.clone() + &str1 {
128+
return String::from("");
129+
}
130+
fn gcd(a: usize, b: usize) -> usize {
131+
if b == 0 {
132+
return a;
133+
}
134+
gcd(b, a % b)
135+
}
136+
137+
let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len()));
138+
str1[..gcd(m, n)].to_string()
139+
}
140+
}
141+
```
142+
122143
### **...**
123144

124145
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn gcd_of_strings(str1: String, str2: String) -> String {
3+
if str1.clone() + &str2 != str2.clone() + &str1 {
4+
return String::from("");
5+
}
6+
fn gcd(a: usize, b: usize) -> usize {
7+
if b == 0 {
8+
return a;
9+
}
10+
gcd(b, a % b)
11+
}
12+
13+
let (m, n) = (str1.len().max(str2.len()), str1.len().min(str2.len()));
14+
str1[..gcd(m, n)].to_string()
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /