Search This Blog

2023/05/27

Longest Common Child String - Recursive

 function commonChild(string1, string2) {

let m = string1.length;
let n = string2.length

if (m <= 0 || n <= 0) {
return 0;
}

for (let i = m - 1; i >= 0; i--) {
for (let j = n - 1; j >= 0; j--) {
if (string1[i] == string2[j]) {
let s1 = string1.substring(0, m - 1);
let s2 = string2.substring(0, n - 1);
return commonChild(s1, s2) + 1
}
else {
let s3 = string1.substring(0, m - 1);
let r1 = commonChild(s3, string2)

let s4 = string2.substring(0, n - 1);
let r2 = commonChild(string1, s4)

let r3 = Math.max(r1, r2)
return r3;
}

}
}
}


const string1 = "ABCD";
const string2 = "ABDC";
const result = commonChild(string1, string2);
console.log("Result:", result);

No comments:

Post a Comment