-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathgreatestCommonDivisor.js
53 lines (42 loc) · 1.36 KB
/
greatestCommonDivisor.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
//1. GCD using Euclidean's algorithm TC: O(n+m)(i.e, O(n + m + log(min(n, m)))), SC: O(n+m)
function gcdOfStrings1(string1, string2){
//Verify whether GCD strings exists or not
if(!(string1+string2 === string2+string1)) {
return false;
}
let gcdLength = gcd(string1.length, string2.length);
return string1.substring(0, gcdLength);
}
function gcd(x, y) {
if(y === 0) {
return x;
}
return gcd(y, x%y);
}
// 2. BrutForce (Time complexity: O(min(m,n) . (m+n)), Space complexity: O(min(m,n)))
function gcdOfStrings2(string1, string2) {
let l1 = string1.length;
let l2 = string2.length;
for(let i= Math.min(l1,l2); i >= 1; i--) {
if(hasGcdString(string1, string2, i)) {
return string1.substring(0, i);
}
}
return "";
}
function hasGcdString(string1, string2, k) {
let l1 = string1.length;
let l2 = string2.length;
if(l1%k > 0 || l2%k >0) {
return false;
}
let f1= l1/k, f2 = l2/k;
let base = string1.substring(0, k);
return base.repeat(f1) === string1 && base.repeat(f2) === string2;
}
console.log(gcdOfStrings1("AB", "AB"));
console.log(gcdOfStrings1("ABCABCABC", "ABCABC"));
console.log(gcdOfStrings1("ABABAB", "AB"));
console.log(gcdOfStrings2("AB", "AB"));
console.log(gcdOfStrings2("ABCABCABC", "ABCABC"));
console.log(gcdOfStrings2("ABABAB", "AB"));