Skip to content

Commit 3fb51f1

Browse files
committed
Create 1071.字符串的最大公因子.js
1 parent be9bb2e commit 3fb51f1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

1071.字符串的最大公因子.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string} str1
3+
* @param {string} str2
4+
* @return {string}
5+
*/
6+
var gcdOfStrings = function(str1, str2) {
7+
let n = Math.min(str1.length, str2.length);
8+
while (n > 0) {
9+
if (str1.length % n === 0 && str2.length % n === 0) {
10+
const sub = str1.substr(0, n);
11+
if (sub.repeat(str1.length / n) === str1 && sub.repeat(str2.length / n) === str2) {
12+
return sub;
13+
}
14+
}
15+
n--;
16+
}
17+
return '';
18+
};

0 commit comments

Comments
 (0)