Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2194 from AkifhanIlgaz/1071
Browse files Browse the repository at this point in the history
Create: 1071-greatest-common-divisor-of-strings
  • Loading branch information
a93a authored Feb 1, 2023
2 parents c95a865 + 1526df3 commit a85c725
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
37 changes: 37 additions & 0 deletions go/1071-greatest-common-divisor-of-strings..go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import "strings"

func main() {

}

func gcdOfStrings(str1 string, str2 string) string {
len1, len2 := len(str1), len(str2)
var isDivisor func(l int) bool

isDivisor = func(l int) bool {
if len1%l != 0 || len2%l != 0 {
return false
}

f1, f2 := len1/l, len2/l

return strings.Repeat(str1[:l], f1) == str1 && strings.Repeat(str1[:l], f2) == str2
}

for l := min(len1,len2); l > 0; l-- {
if isDivisor(l) {
return str1[:l]
}
}

return ""
}

func min( a,b int) int {
if a < b {
return a
}
return b
}
28 changes: 28 additions & 0 deletions javascript/1071-greatest-common-divisor-of-strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
var gcdOfStrings = function (str1, str2) {
let [len1, len2] = [str1.length, str2.length];

function isDivisor(l) {
if (len1 % l || len2 % l) {
return false;
}

let [f1, f2] = [Math.floor(len1 / l), Math.floor(len2 / l)];

return (
str1.slice(0, l).repeat(f1) == str1 && str1.slice(0, l).repeat(f2) == str2
);
}

for (let l = Math.min(len1, len2); l > 0; l--) {
if (isDivisor(l)) {
return str1.slice(0, l);
}
}

return "";
};
21 changes: 21 additions & 0 deletions rust/1071-greatest-common-divisor-of-strings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
pub fn gcd_of_strings(str1: String, str2: String) -> String {
let (len1, len2) = (str1.len(), str2.len());

for l in (1..=usize::min(len1, len2)).rev() {
if Self::is_divisor(&str1, &str2, len1, len2, l) {
return str1[..l].to_string();
}
}

"".to_string()
}

pub fn is_divisor(str1: &String, str2: &String, len1: usize, len2: usize, l: usize) -> bool {
if len1 % l != 0 || len2 % l != 0 {
return false;
}
let (f1, f2) = (len1 / l, len2 / l);
str1[..l].repeat(f1) == *str1 && str1[..l].repeat(f2) == *str2
}
}
24 changes: 24 additions & 0 deletions typescript/1071-greatest-common-divisor-of-strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function gcdOfStrings(str1: string, str2: string): string {
let [len1, len2] = [str1.length, str2.length];

function isDivisor(l: number): boolean {
if (len1 % l || len2 % l) {
return false;
}

let [f1, f2] = [Math.floor(len1 / l), Math.floor(len2 / l)];

return (
str1.slice(0, l).repeat(f1) == str1 &&
str1.slice(0, l).repeat(f2) == str2
);
}

for (let l = Math.min(len1, len2); l > 0; l--) {
if (isDivisor(l)) {
return str1.slice(0, l);
}
}

return '';
}

0 comments on commit a85c725

Please sign in to comment.