This commit is contained in:
Vikramaditya Kokil 2024-07-17 10:39:24 +09:00 committed by GitHub
commit 99c3d5e678
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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;