Complete the code to initialize the 2D array for dynamic programming.
const dp = Array(s1.length + 1).fill(null).map(() => Array([1]).fill(0));
The 2D array dp needs to have rows for each character in s1 plus one extra for empty substring, and columns for s2 length plus one. So dp has dimensions (s1.length + 1) x (s2.length + 1).
Complete the condition to check if characters match at positions i-1 and j-1.
if (s1[i - 1] [1] s2[j - 1]) {
We use strict equality (===) in JavaScript to check if characters are exactly the same.
Fix the error in updating dp[i][j] when characters match.
dp[i][j] = dp[i - 1][j - 1] [1] 1;
When characters match, we extend the substring length by 1, so we add 1 to dp[i-1][j-1].
Fill both blanks to update maxLength and endIndex when a longer substring is found.
if (dp[i][j] [1] maxLength) { maxLength = dp[i][j]; endIndex = i [2] 1; }
We update maxLength if dp[i][j] is greater than current maxLength. endIndex is set to i - 1 because i is 1-based index in dp array.
Fill all three blanks to return the longest common substring from s1.
return s1.slice(endIndex [1] maxLength [2] 1, endIndex [3] 1);
The slice start index is endIndex - maxLength, and end index is endIndex + maxLength is incorrect. We want endIndex + 1 as end index for slice. So correct is slice(endIndex - maxLength + 1, endIndex + 1). Here blanks fill the start and end indices correctly.
