Question

What are the time and space complexities of the standard dynamic programming approach for finding the length of the Longest Common Subsequence (LC

  • S of two strings, text1 of length m and text2 of length n? Consider the following Python code: def lcs_length(text1, text2):     m = len(text1)     n = len(text2)     dp = [[0] * (n + 1) for _ in range(m + 1)] # Space complexity     for i in range(1, m + 1): # Outer loop runs m times         for j in range(1, n + 1): # Inner loop runs n times             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]
A Time: O(m + n), Space: O(m + n)
B Time: O(m * n), Space: O(m + n)
C Time: O(m * n), Space: O(m * n)
D Time: O(2^(m+n)), Space: O(m * n)
Practice Next

Hey! Ask a query