Back to all solutions
#1071 - Greatest Common Divisor of Strings
Problem Description
For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times).
Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.
Solution
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
var gcdOfStrings = function(str1, str2) {
if (str1 + str2 !== str2 + str1) return '';
for (let i = str2.length; i >= 0; i--) {
if (str1.length % i === 0 && str2.length % i === 0) {
return str1.substring(0, i);
}
}
};