-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (53 loc) · 1.41 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
function createArray(dimension) {
var array = [];
for (var i = 0; i < dimension; i++) {
array[i] = [];
}
return array;
}
module.exports = function (firstSequence, secondSequence, caseSensitive) {
var firstString = caseSensitive ? firstSequence : firstSequence.toLowerCase();
var secondString = caseSensitive ? secondSequence : secondSequence.toLowerCase();
if (firstString === secondString) {
return firstString;
}
if ((firstString || secondString) === '') {
return '';
}
var firstStringLength = firstString.length;
var secondStringLength = secondString.length;
var lcsMatrix = createArray(secondStringLength + 1);
var i;
var j;
for (i = 0; i <= firstStringLength; i++) {
lcsMatrix[0][i] = 0;
}
for (i = 0; i <= secondStringLength; i++) {
lcsMatrix[i][0] = 0;
}
for (i = 1; i <= secondStringLength; i++) {
for (j = 1; j <= firstStringLength; j++) {
if (firstString[j - 1] === secondString[i - 1]) {
lcsMatrix[i][j] = lcsMatrix[i - 1][j - 1] + 1;
} else {
lcsMatrix[i][j] = Math.max(lcsMatrix[i - 1][j], lcsMatrix[i][j - 1]);
}
}
}
var lcs = "";
i = secondStringLength;
j = firstStringLength;
while (i > 0 && j > 0) {
if (firstString[j - 1] === secondString[i - 1]) {
lcs = firstString[j - 1] + lcs;
i--;
j--;
} else if (Math.max(lcsMatrix[i - 1][j], lcsMatrix[i][j - 1]) === lcsMatrix[i - 1][j]) {
i--;
} else {
j--;
}
}
return lcs;
};