-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_209.js
51 lines (47 loc) Β· 1.09 KB
/
problem_209.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
/**
* Find the longest common subsequence of three strings
* @param {string} s1
* @param {string} s2
* @param {string} s3
* @return {string}
*/
function longestSequence(s1, s2, s3) {
// Find length of each string
const m = s1.length;
const n = s2.length;
const o = s3.length;
// Declare 3D array
const LCS = Array(m)
.fill(0)
.map(() =>
Array(n)
.fill(0)
.map(() =>
Array(o)
.fill(0)
.map(e => e)
)
);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
for (let k = 0; k < o; k++) {
if (i === 0 || j === 0 || k === 0) LCS[i][j][k] = 0;
else if (s1[i - 1] === s2[j - 1] && s1[i - 1] === s3[k - 1])
LCS[i][j][k] = LCS[i - 1][j - 1][k - 1] + 1;
else
LCS[i][j][k] = Math.max(
Math.max(LCS[i - 1][j][k], LCS[i][j - 1][k]),
LCS[i][j][k - 1]
);
}
}
}
return LCS[m - 1][n - 1][o - 1];
}
console.log(
longestSequence(
'epidemiologist',
'refrigeration',
'supercalifragilisticexpialodocious'
)
); // 5