Greatest Common Divisor of Strings LeetCode solution in JavaScript

1
/**
2
* @param {string} str1
3
* @param {string} str2
4
* @return {string}
5
*/
6
var gcdOfStrings = function (str1, str2) {
7
const gcdOfNumber = (num1, num2) => {
8
while (num2 !== 0) {
9
const res = num2 % num1;
10
num1 = num2;
11
num2 = res;
12
}
13
return num1;
14
};
15
const gcd = gcdOfNumber(str1.length, str2.length);
16
const common = str1.slice(0, gcd);
17
return common.repeat(str1.length / gcd) === str1 &&
18
common.repeat(str2.length / gcd) === str2
19
? common
20
: "";
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0