Skip to content

Commit 1e7dbca

Browse files
committed
✅ [1071] factors method!! MY 100TH QN!!!!
1 parent 9278f2e commit 1e7dbca

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

1071/my_solution.js

+39-40
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,59 @@ var gcdOfStrings = function (str1, str2) {
77
// must be the same after merging before/after, if they have common divider.
88
if ((str1 + str2) !== (str2 + str1)) return "";
99

10-
const findGCD = (str) => {
11-
console.log(`________________________________`)
12-
let gcd = null, tmpGcd = str[0], l = 0, r = 1
1310

14-
while (r < str.length) {
15-
console.log(gcd)
11+
const calculateFactors = (n) => {
12+
let factors = []
1613

17-
let curr = str[r]
18-
19-
console.log(`l:${l},r:${r},gcd:${gcd},tmpGcd:${tmpGcd}`)
14+
for (let i = n; i > 0; i--) { // <= to include itself or start from the back to get the reversed version
15+
if ((n % i) === 0) factors.push(i)
16+
}
2017

21-
if (curr === str[l]) {
22-
console.log(`curr:${curr} === str[l]:${str[l]}`)
23-
console.log(`gcd:${gcd},tmpGcd:${tmpGcd}`)
18+
return factors;
19+
}
2420

25-
if (gcd !== null && tmpGcd.length !== gcd.length) {
26-
console.log(`EDGE:`)
27-
console.log(gcd)
28-
console.log(tmpGcd)
29-
return ""; // edge case when tmp gcd is not the same
30-
}
21+
let f1 = calculateFactors(str1.length), f2 = calculateFactors(str2.length), greatestCommonFactor = 0
3122

32-
gcd = tmpGcd;
33-
tmpGcd = curr;
34-
// l = r;
35-
} else {
36-
tmpGcd += curr;
37-
}
23+
let listToFindGCF = (f1.length > f2.length) ? f2 : f1; // use the shortest
24+
let largerFactorList = (f1.length > f2.length) ? f1 : f2;
25+
let gcf = null; // greatest common factor
3826

39-
console.log(`-> end tmpGcd:${tmpGcd}`)
40-
r++;
41-
}
27+
console.log(`-> f1: ${f1}, f2: ${f2}, gcf: ${gcf}`)
28+
while (gcf === null) {
29+
let curr = listToFindGCF.shift();
30+
if (largerFactorList.includes(curr)) gcf = curr;
31+
}
4232

43-
console.log(`-> final gcd:${gcd}, tmpGcd:${tmpGcd}`)
44-
console.log(gcd === null ? tmpGcd : gcd)
33+
let multiplier = ((str2.length > str1.length) ? str2.length : str1.length) / gcf;
34+
console.log(`--> f1: ${f1}, f2: ${f2}, gcf: ${gcf}`)
4535

46-
return gcd === null ? tmpGcd : gcd;
36+
// get the part
37+
// validate by their multiplier
38+
const validateByGCF = (str) => {
39+
let multiplier = str.length / gcf;
40+
console.log(`---> multiplier: ${multiplier}`)
41+
// NOTE: slice is inclusive
42+
let slicedStr = str.slice(0, gcf), multipliedStr = "";
43+
while (multiplier > 0) {
44+
multipliedStr += slicedStr;
45+
multiplier--;
46+
}
47+
console.log(`str: ${str}, slicedStr: ${slicedStr}, mstr: ${multipliedStr}`)
48+
return (multipliedStr === str) ? slicedStr : "";
4749
}
4850

49-
let
50-
gcd1 = findGCD(str1),
51-
gcd2 = findGCD(str2);
52-
console.log("gcd1")
53-
console.log(gcd1)
54-
console.log("gcd2")
55-
console.log(gcd2)
56-
return gcd1 == gcd2 ? gcd1 : "";
51+
return validateByGCF(str1); // either one will do as long as we get the gcf
52+
let gcd1 = validateByGCF(str1), gcd2 = validateByGCF(str2);
53+
// return (gcd1===gcd2)?
54+
5755

5856
};
5957

6058
let x =
61-
// gcdOfStrings("ABCABC", "ABC")
62-
// gcdOfStrings("ABABAB", "ABAB")
63-
gcdOfStrings("ABABABAB", "ABAB")
59+
gcdOfStrings("ABCABC", "ABC")
60+
// gcdOfStrings("ABABAB", "ABAB")
61+
// gcdOfStrings("ABCDABCDABCDABCD", "ABCDABCD")
62+
// gcdOfStrings("ABABABAB", "ABAB")
6463

6564
console.log("Result")
6665
console.log(x)

1071/solution.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,38 @@
44
* @return {string}
55
*/
66
var gcdOfStrings = function (str1, str2) {
7+
// must be the same after merging before/after, if they have common divider.
8+
if ((str1 + str2) !== (str2 + str1)) return "";
79

8-
const findGCD = (str) => {
9-
let gcd = null, tmpGcd = str[0], l = 0, r = 1
10+
const calculateFactors = (n) => {
11+
let factors = []
1012

11-
while (r < str.length) {
12-
let curr = str[r]
13-
14-
if (curr === str[l]) {
15-
if (gcd !== null && tmpGcd.length !== gcd.length) return ""; // edge case when tmp gcd is not the same
16-
17-
gcd = tmpGcd;
18-
tmpGcd = curr;
19-
// l = r;
20-
} else {
21-
tmpGcd += curr;
22-
}
23-
24-
r++;
13+
for (let i = n; i > 0; i--) { // <= to include itself or start from the back to get the reversed version
14+
if ((n % i) === 0) factors.push(i)
2515
}
2616

27-
return gcd === null ? tmpGcd : gcd;
17+
return factors;
2818
}
2919

3020
let
31-
gcd1 = findGCD(str1),
32-
gcd2 = findGCD(str2);
21+
f1 = calculateFactors(str1.length), f2 = calculateFactors(str2.length),
22+
listToFindGCF = (f1.length > f2.length) ? f2 : f1, // use the shortest
23+
largerFactorList = (f1.length > f2.length) ? f1 : f2,
24+
gcf = null; // greatest common factor
25+
26+
while (gcf === null) {
27+
let curr = listToFindGCF.shift();
28+
if (largerFactorList.includes(curr)) gcf = curr;
29+
}
3330

34-
return gcd1 == gcd2 ? gcd1 : "";
31+
const validateByGCF = (str) => {
32+
let multiplier = str.length / gcf, slicedStr = str.slice(0, gcf), multipliedStr = "";
33+
while (multiplier > 0) {
34+
multipliedStr += slicedStr;
35+
multiplier--;
36+
}
37+
return (multipliedStr === str) ? slicedStr : "";
38+
}
3539

40+
return validateByGCF(str1); // either one will do as long as we get the gcf
3641
};

0 commit comments

Comments
 (0)