File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 262
262
1022|[ Sum of Root To Leaf Binary Numbers] ( ./1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
263
263
1037|[ Valid Boomerang] ( ./1037-valid-boomerang.js ) |Easy|
264
264
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|
265
266
1081|[ Smallest Subsequence of Distinct Characters] ( ./1081-smallest-subsequence-of-distinct-characters.js ) |Medium|
266
267
1103|[ Distribute Candies to People] ( ./1103-distribute-candies-to-people.js ) |Easy|
267
268
1108|[ Defanging an IP Address] ( ./1108-defanging-an-ip-address.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments