Compare commits

...

2 Commits

Author SHA1 Message Date
Vikramaditya Kokil
519f705b54
Merge b9a0f565f7 into 2c67b48c21 2024-04-25 08:20:38 +08:00
vk92kokil
b9a0f565f7 Remove extra init loops in lcs 2020-09-07 13:27:50 +05:30

View File

@ -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;