Skip to content

Commit 9ea900a

Browse files
committed
feat: add solutions to lc problem: No.1071
No.1071.Greatest Common Divisor of Strings
1 parent dfeebfa commit 9ea900a

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,80 @@
5353
<!-- 这里可写当前语言的特殊实现逻辑 -->
5454

5555
```python
56+
class Solution:
57+
def gcdOfStrings(self, str1: str, str2: str) -> str:
58+
def check(a, b):
59+
c = ""
60+
while len(c) < len(b):
61+
c += a
62+
return c == b
63+
64+
for i in range(min(len(str1), len(str2)), 0, -1):
65+
t = str1[:i]
66+
if check(t, str1) and check(t, str2):
67+
return t
68+
return ''
69+
```
5670

71+
```python
72+
class Solution:
73+
def gcdOfStrings(self, str1: str, str2: str) -> str:
74+
if str1 + str2 != str2 + str1:
75+
return ''
76+
n = gcd(len(str1), len(str2))
77+
return str1[:n]
5778
```
5879

5980
### **Java**
6081

6182
<!-- 这里可写当前语言的特殊实现逻辑 -->
6283

6384
```java
85+
class Solution {
86+
public String gcdOfStrings(String str1, String str2) {
87+
if (!(str1 + str2).equals(str2 + str1)) {
88+
return "";
89+
}
90+
int len = gcd(str1.length(), str2.length());
91+
return str1.substring(0, len);
92+
}
93+
94+
private int gcd(int a, int b) {
95+
return b == 0 ? a : gcd(b, a % b);
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
string gcdOfStrings(string str1, string str2) {
106+
if (str1 + str2 != str2 + str1) return "";
107+
int n = __gcd(str1.size(), str2.size());
108+
return str1.substr(0, n);
109+
}
110+
};
111+
```
64112
113+
### **Go**
114+
115+
```go
116+
func gcdOfStrings(str1 string, str2 string) string {
117+
if str1+str2 != str2+str1 {
118+
return ""
119+
}
120+
n := gcd(len(str1), len(str2))
121+
return str1[:n]
122+
}
123+
124+
func gcd(a, b int) int {
125+
if b == 0 {
126+
return a
127+
}
128+
return gcd(b, a%b)
129+
}
65130
```
66131

67132
### **...**

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,78 @@
4545
### **Python3**
4646

4747
```python
48+
class Solution:
49+
def gcdOfStrings(self, str1: str, str2: str) -> str:
50+
def check(a, b):
51+
c = ""
52+
while len(c) < len(b):
53+
c += a
54+
return c == b
55+
56+
for i in range(min(len(str1), len(str2)), 0, -1):
57+
t = str1[:i]
58+
if check(t, str1) and check(t, str2):
59+
return t
60+
return ''
61+
```
4862

63+
```python
64+
class Solution:
65+
def gcdOfStrings(self, str1: str, str2: str) -> str:
66+
if str1 + str2 != str2 + str1:
67+
return ''
68+
n = gcd(len(str1), len(str2))
69+
return str1[:n]
4970
```
5071

5172
### **Java**
5273

5374
```java
75+
class Solution {
76+
public String gcdOfStrings(String str1, String str2) {
77+
if (!(str1 + str2).equals(str2 + str1)) {
78+
return "";
79+
}
80+
int len = gcd(str1.length(), str2.length());
81+
return str1.substring(0, len);
82+
}
83+
84+
private int gcd(int a, int b) {
85+
return b == 0 ? a : gcd(b, a % b);
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
string gcdOfStrings(string str1, string str2) {
96+
if (str1 + str2 != str2 + str1) return "";
97+
int n = __gcd(str1.size(), str2.size());
98+
return str1.substr(0, n);
99+
}
100+
};
101+
```
54102
103+
### **Go**
104+
105+
```go
106+
func gcdOfStrings(str1 string, str2 string) string {
107+
if str1+str2 != str2+str1 {
108+
return ""
109+
}
110+
n := gcd(len(str1), len(str2))
111+
return str1[:n]
112+
}
113+
114+
func gcd(a, b int) int {
115+
if b == 0 {
116+
return a
117+
}
118+
return gcd(b, a%b)
119+
}
55120
```
56121

57122
### **...**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
string gcdOfStrings(string str1, string str2) {
4+
if (str1 + str2 != str2 + str1) return "";
5+
int n = __gcd(str1.size(), str2.size());
6+
return str1.substr(0, n);
7+
}
8+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func gcdOfStrings(str1 string, str2 string) string {
2+
if str1+str2 != str2+str1 {
3+
return ""
4+
}
5+
n := gcd(len(str1), len(str2))
6+
return str1[:n]
7+
}
8+
9+
func gcd(a, b int) int {
10+
if b == 0 {
11+
return a
12+
}
13+
return gcd(b, a%b)
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def gcdOfStrings(self, str1: str, str2: str) -> str:
3+
if str1 + str2 != str2 + str1:
4+
return ''
5+
n = gcd(len(str1), len(str2))
6+
return str1[:n]

0 commit comments

Comments
 (0)