-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon-child.js
35 lines (29 loc) · 877 Bytes
/
common-child.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
/**
* @title Common Child
* @difficulty Medium
* @link https://www.hackerrank.com/challenges/common-child/problem
*/
const lca = (string1, string2) => {
const s1 = (string1.length <= string2.length) ? [...string1] : [...string2];
const s2 = (string1.length <= string2.length) ? [...string2] : [...string1];
const {length: rowLength} = s1;
const {length: colLength} = s2;
let row = s1.reduce(acc => {
acc.push(0);
return acc;
}, []);
row.push(0);
const cur = [...row];
for (let idxS2 = 0; idxS2 < colLength; idxS2++) {
for (let idxS1 = 0; idxS1 < rowLength; idxS1++) {
if (s1[idxS1] === s2[idxS2]) {
cur[idxS1 + 1] = row[idxS1] + 1;
} else {
cur[idxS1 + 1] = (Math.max(cur[idxS1], row[idxS1 + 1]));
}
}
row = [...cur];
}
return cur[rowLength];
};
const commonChild = (s1, s2) => lca(s1, s2);