Question

In the dynamic programming approach for LCS, the base cases are crucial for correctly initializing the dp table. Consider the following Python code snippet: def lcs_length(text1, text2):     m = len(text1)     n = len(text2)     dp = [[0] * (n + 1) for _ in range(m + 1)]     # The loops start from 1, effectively using dp[0][j] and dp[i][0] as base cases.     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] If text1 = "" (an empty string) and text2 = "ABCD", what will be the final result returned by lcs_length(text1, text2)?

A 4
C Error (Index out of bounds)
D 1
Practice Next

Hey! Ask a query