mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Compare commits
2 Commits
99c3d5e678
...
519f705b54
Author | SHA1 | Date | |
---|---|---|---|
|
519f705b54 | ||
|
b9a0f565f7 |
@ -14,18 +14,7 @@ export default function longestCommonSubstring(string1, string2) {
|
|||||||
const s2 = [...string2];
|
const s2 = [...string2];
|
||||||
|
|
||||||
// Init the matrix of all substring lengths to use Dynamic Programming approach.
|
// Init the matrix of all substring lengths to use Dynamic Programming approach.
|
||||||
const substringMatrix = Array(s2.length + 1).fill(null).map(() => {
|
const substringMatrix = Array(s2.length + 1).fill(0).map(() => Array(s1.length + 1).fill(0));
|
||||||
return Array(s1.length + 1).fill(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fill the first row and first column with zeros to provide initial values.
|
|
||||||
for (let columnIndex = 0; columnIndex <= s1.length; columnIndex += 1) {
|
|
||||||
substringMatrix[0][columnIndex] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let rowIndex = 0; rowIndex <= s2.length; rowIndex += 1) {
|
|
||||||
substringMatrix[rowIndex][0] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build the matrix of all substring lengths to use Dynamic Programming approach.
|
// Build the matrix of all substring lengths to use Dynamic Programming approach.
|
||||||
let longestSubstringLength = 0;
|
let longestSubstringLength = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user