Complete the code to initialize the 2D slice for dynamic programming.
dp := make([][]int, len(s1)+1) for i := range dp { dp[i] = make([]int, [1]) }
We create a 2D slice with rows for s1 and columns for s2 plus one extra for base case.
Complete the code to update the dp table when characters match.
if s1[i-1] == s2[j-1] { dp[i][j] = dp[i-1][[1]] + 1 }
When characters match, we extend the substring by 1 from the previous diagonal cell dp[i-1][j-1].
Fix the error in updating the maximum length of the common substring.
if dp[i][j] > [1] { maxLen = dp[i][j] endIndex = i }
We compare dp[i][j] with maxLen to update the maximum length found so far.
Fill both blanks to extract the longest common substring from s1.
result := s1[[1]:[2]]
The substring starts at endIndex - maxLen and ends at endIndex (exclusive).
Fill all three blanks to complete the nested loops for filling dp table.
for i := 1; i <= len(s1); i++ { for j := 1; j <= [1]; j++ { if s1[i-1] == s2[j-1] { dp[i][j] = dp[i-1][[2]] + 1 if dp[i][j] > maxLen { maxLen = dp[i][j] endIndex = i } } else { dp[i][j] = [3] } } }
The inner loop runs over s2 length. We use dp[i-1][j-1] for previous diagonal. When no match, dp[i][j] is 0.
