Skip to content

Commit a85c725

Browse files
authored
Merge pull request #2194 from AkifhanIlgaz/1071
Create: 1071-greatest-common-divisor-of-strings
2 parents c95a865 + 1526df3 commit a85c725

4 files changed

+110
-0
lines changed

Diff for: go/1071-greatest-common-divisor-of-strings..go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "strings"
4+
5+
func main() {
6+
7+
}
8+
9+
func gcdOfStrings(str1 string, str2 string) string {
10+
len1, len2 := len(str1), len(str2)
11+
var isDivisor func(l int) bool
12+
13+
isDivisor = func(l int) bool {
14+
if len1%l != 0 || len2%l != 0 {
15+
return false
16+
}
17+
18+
f1, f2 := len1/l, len2/l
19+
20+
return strings.Repeat(str1[:l], f1) == str1 && strings.Repeat(str1[:l], f2) == str2
21+
}
22+
23+
for l := min(len1,len2); l > 0; l-- {
24+
if isDivisor(l) {
25+
return str1[:l]
26+
}
27+
}
28+
29+
return ""
30+
}
31+
32+
func min( a,b int) int {
33+
if a < b {
34+
return a
35+
}
36+
return b
37+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} str1
3+
* @param {string} str2
4+
* @return {string}
5+
*/
6+
var gcdOfStrings = function (str1, str2) {
7+
let [len1, len2] = [str1.length, str2.length];
8+
9+
function isDivisor(l) {
10+
if (len1 % l || len2 % l) {
11+
return false;
12+
}
13+
14+
let [f1, f2] = [Math.floor(len1 / l), Math.floor(len2 / l)];
15+
16+
return (
17+
str1.slice(0, l).repeat(f1) == str1 && str1.slice(0, l).repeat(f2) == str2
18+
);
19+
}
20+
21+
for (let l = Math.min(len1, len2); l > 0; l--) {
22+
if (isDivisor(l)) {
23+
return str1.slice(0, l);
24+
}
25+
}
26+
27+
return "";
28+
};

Diff for: rust/1071-greatest-common-divisor-of-strings.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn gcd_of_strings(str1: String, str2: String) -> String {
3+
let (len1, len2) = (str1.len(), str2.len());
4+
5+
for l in (1..=usize::min(len1, len2)).rev() {
6+
if Self::is_divisor(&str1, &str2, len1, len2, l) {
7+
return str1[..l].to_string();
8+
}
9+
}
10+
11+
"".to_string()
12+
}
13+
14+
pub fn is_divisor(str1: &String, str2: &String, len1: usize, len2: usize, l: usize) -> bool {
15+
if len1 % l != 0 || len2 % l != 0 {
16+
return false;
17+
}
18+
let (f1, f2) = (len1 / l, len2 / l);
19+
str1[..l].repeat(f1) == *str1 && str1[..l].repeat(f2) == *str2
20+
}
21+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function gcdOfStrings(str1: string, str2: string): string {
2+
let [len1, len2] = [str1.length, str2.length];
3+
4+
function isDivisor(l: number): boolean {
5+
if (len1 % l || len2 % l) {
6+
return false;
7+
}
8+
9+
let [f1, f2] = [Math.floor(len1 / l), Math.floor(len2 / l)];
10+
11+
return (
12+
str1.slice(0, l).repeat(f1) == str1 &&
13+
str1.slice(0, l).repeat(f2) == str2
14+
);
15+
}
16+
17+
for (let l = Math.min(len1, len2); l > 0; l--) {
18+
if (isDivisor(l)) {
19+
return str1.slice(0, l);
20+
}
21+
}
22+
23+
return '';
24+
}

0 commit comments

Comments
 (0)