Skip to content

Commit 7f65a2b

Browse files
committed
Add solution #1071
1 parent fb8d28c commit 7f65a2b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
1022|[Sum of Root To Leaf Binary Numbers](./1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
263263
1037|[Valid Boomerang](./1037-valid-boomerang.js)|Easy|
264264
1041|[Robot Bounded In Circle](./1041-robot-bounded-in-circle.js)|Medium|
265+
1071|[Greatest Common Divisor of Strings](./1071-greatest-common-divisor-of-strings.js)|Easy|
265266
1081|[Smallest Subsequence of Distinct Characters](./1081-smallest-subsequence-of-distinct-characters.js)|Medium|
266267
1103|[Distribute Candies to People](./1103-distribute-candies-to-people.js)|Easy|
267268
1108|[Defanging an IP Address](./1108-defanging-an-ip-address.js)|Easy|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 1071. Greatest Common Divisor of Strings
3+
* https://leetcode.com/problems/greatest-common-divisor-of-strings/
4+
* Difficulty: Easy
5+
*
6+
* For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t
7+
* (i.e., t is concatenated with itself one or more times).
8+
*
9+
* Given two strings str1 and str2, return the largest string x such that x divides both str1
10+
* and str2.
11+
*/
12+
13+
/**
14+
* @param {string} str1
15+
* @param {string} str2
16+
* @return {string}
17+
*/
18+
var gcdOfStrings = function(str1, str2) {
19+
if (str1 + str2 !== str2 + str1) return '';
20+
21+
for (let i = str2.length; i >= 0; i--) {
22+
if (str1.length % i === 0 && str2.length % i === 0) {
23+
return str1.substring(0, i);
24+
}
25+
}
26+
};

0 commit comments

Comments
 (0)