-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1071-greatest-common-divisor-of-strings.js
207 lines (172 loc) · 7.22 KB
/
1071-greatest-common-divisor-of-strings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 56 ms, faster than 70.42% of JavaScript online submissions
// Memory Usage: 34 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
// const gcdOfStrings = (str1, str2) => {
// const findGcd = (a, b) => (0 === b ? a : findGcd(b, a % b));
// const gcd = findGcd(str1.length, str2.length);
// const combo = str1 + str2;
// const gcdString = combo.substring(0, gcd);
// for (let i = gcd; i < combo.length; i += gcd) {
// if (gcdString !== combo.substring(i, i + gcd)) return '';
// }
// return gcdString;
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 60 ms, faster than 49.30% of JavaScript online submissions
// Memory Usage: 35.3 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
// const gcdOfStrings = (str1, str2) => {
// const findGcd = (a, b) => (0 === b ? a : findGcd(b, a % b));
// const gcd = findGcd(str1.length, str2.length);
// const combo = str1 + str2;
// for (let i = 0; i < gcd; i++) {
// for (let j = i + gcd; j < combo.length; j += gcd) {
// if (combo.charAt(i) !== combo.charAt(j)) return '';
// }
// }
// return combo.substring(0, gcd);
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 60 ms, faster than 49.30% of JavaScript online submissions
// Memory Usage: 35.3 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
// const gcdOfStrings = (str1, str2) => {
// const findGcd = (a, b) => (0 === b ? a : findGcd(b, a % b));
// const gcd = findGcd(str1.length, str2.length);
// for (let i = 0; i < gcd; i++) {
// for (let j = i + gcd; j < str1.length; j += gcd) {
// if (str1.charAt(i) !== str1.charAt(j)) return '';
// }
// for (let j = i; j < str2.length; j += gcd) {
// if (str1.charAt(i) !== str2.charAt(j)) return '';
// }
// }
// return str1.substring(0, gcd);
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 68 ms, faster than 25.35% of JavaScript online submissions
// Memory Usage: 33.8 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
// const gcdOfStrings = (str1, str2) => {
// const findGcd = (a, b) => (0 === b ? a : findGcd(b, a % b));
// const gcd = findGcd(str1.length, str2.length);
// const gcdString = str1.substring(0, gcd);
// if (0 < str2.replace(new RegExp(gcdString, 'g'), '').length) return '';
// return gcdString;
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 52 ms, faster than 91.55% of JavaScript online submissions
// Memory Usage: 33.8 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
// const gcdOfStrings = (str1, str2) => {
// const len1 = str1.length;
// const len2 = str2.length;
// if (len1 < len2) return gcdOfStrings(str2, str1);
// if (len1 === len2 && str1 !== str2) return '';
// if (str1 !== str1.substring(len2, len1) + str2) return '';
// const findGcd = (a, b) => (0 === b ? a : findGcd(b, a % b));
// return str1.substring(0, findGcd(str1.length, str2.length));
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 60 ms, faster than 49.30% of JavaScript online submissions
// Memory Usage: 33.7 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
// const gcdOfStrings = (str1, str2) => {
// const len1 = str1.length;
// const len2 = str2.length;
// if (
// (len1 === len2 && str1 !== str2) ||
// (len2 < len1 && str1 !== str1.substring(len2, len1) + str2) ||
// (len1 < len2 && str2 !== str2.substring(len2, len1) + str1)
// ) {
// return '';
// }
// const findGcd = (a, b) => (0 === b ? a : findGcd(b, a % b));
// return str1.substring(0, findGcd(str1.length, str2.length));
// };
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 48 ms, faster than 95.77% of JavaScript online submissions
// Memory Usage: 33.8 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} str1
* @param {string} str2
* @return {string}
*/
const gcdOfStrings = (str1, str2) => {
if (str1 + str2 !== str2 + str1) return '';
const gcd = (a, b) => (0 === b ? a : gcd(b, a % b));
return str1.substring(0, gcd(str1.length, str2.length));
};
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 112 ms, faster than 7.43% of JavaScript online submissions
// Memory Usage: 33.6 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number} x
// * @param {number} y
// * @return {number} The greatest common denominator
// */
// const gcd = (x, y) => (0 === y ? x : gcd(y, x % y));
// /**
// * @param {string} str1
// * @param {string} str2
// * @return {string}
// */
// const gcdOfStrings = (str1, str2) =>
// str1 + str2 !== str2 + str1
// ? ''
// : str1.substring(0, gcd(str1.length, str2.length));
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import { strictEqual } from 'assert';
strictEqual(gcdOfStrings('ABCABC', 'ABC'), 'ABC');
strictEqual(gcdOfStrings('ABABAB', 'ABAB'), 'AB');
strictEqual(gcdOfStrings('LEET', 'CODE'), '');
strictEqual(
gcdOfStrings(
'TAUXXTAUXXTAUXXTAUXXTAUXX',
'TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX',
),
'TAUXX',
);
strictEqual(gcdOfStrings('ABABAB', 'ABCD'), '');
strictEqual(gcdOfStrings('ABCD', 'ABABAB'), '');
strictEqual(
gcdOfStrings(
'NLZGMNLZGMNLZGMNLZGMNLZGMNLZGMNLZGMNLZGM',
'NLZGMNLZGMNLZGMNLZGMNLZGMNLZGMNLZGMNLZGMNLZGM',
),
'NLZGM',
);
strictEqual(
gcdOfStrings(
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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