Complete the code to initialize the 2D list for dynamic programming.
dp = [[0 for _ in range(len(s2) + 1)] for _ in range([1] + 1)]
We create a 2D list with rows equal to length of s1 plus one, to include the empty prefix.
Complete the code to check if characters match at current indices.
if s1[i - 1] [1] s2[j - 1]:
We check if characters at positions i-1 and j-1 are equal to extend the subsequence.
Fix the error in updating dp when characters do not match.
dp[i][j] = max(dp[i - 1][j], dp[i][[1]])
When characters don't match, we take the max of dp[i-1][j] and dp[i][j-1].
Fill both blanks to complete the loop headers for building dp table.
for i in range(1, [1] + 1): for j in range(1, [2] + 1):
We loop from 1 to length of s1 and s2 to fill dp table.
Fill all three blanks to reconstruct the LCS string from dp table.
while i > 0 and j > 0: if s1[i - 1] == s2[j - 1]: lcs = s1[[1]] + lcs i -= [2] j -= [3] elif dp[i - 1][j] > dp[i][j - 1]: i -= 1 else: j -= 1
We add character at i-1, then move diagonally up-left by decreasing i and j by 1.
