Question

Consider the following Python code for calculating the length of the LCS: def lcs_length(text1, text2):     m = len(text1)     n = len(text2)     dp = [[0] * (n + 1) for _ in range(m + 1)]     for i in range(1, m + 1):         for j in range(1, n + 1):             if text1[i - 1] == text2[j - 1]:                 dp[i][j] = 1 + dp[i - 1][j - 1]             else:                 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])     return dp[m][n] # Assume text1 = "AGGTAB" and text2 = "GXTXAYB" # And the dp table has been partially filled as follows (only relevant cells shown): #       ""  G   X   T   X   A   Y   B # ""    0   0   0   0   0   0   0   0 # A     0   0   0   0   0   1   1   1 # G     0   1   1   1   1   1   1   1 # G     0   1   1   1   1   1   1   1 # T     0   1   1   2   2   2   2   2 # A     0   1   1   2   2   3   3   3 # B     0   1   1   2   2   3   3   4 

A 1
B 2
C 3
D 4
Practice Next

Hey! Ask a query