-
Notifications
You must be signed in to change notification settings - Fork 0
/
1237.js
35 lines (24 loc) · 1.07 KB
/
1237.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
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
function longestCommonSubstring(string1, string2, string1Size, string2Size) {
let array = new Array(string1Size + 1), lenghtOfLongestCommonSubstring = 0;
for (let i = 0; i < array.length; i++) {
array[i] = new Array(string2Size + 1);
}
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array[i].length; j++) {
if (i === 0 || j === 0) {
array[i][j] = 0;
} else if (string1[i - 1] === string2[j - 1]) {
array[i][j] = array[i - 1][j - 1] + 1;
lenghtOfLongestCommonSubstring = array[i][j] > lenghtOfLongestCommonSubstring ? array[i][j] : lenghtOfLongestCommonSubstring;
} else {
array[i][j] = 0;
}
}
}
return lenghtOfLongestCommonSubstring;
}
for (let i = 0; i < lines.length - 1; i += 2) {
console.log(longestCommonSubstring(lines[i], lines[i + 1], lines[i].length, lines[i + 1].length));
}