longest-common-subsequect
Problem
Problem Description
Solution
This is LCS, a Dynamic Programming (DP) problem, which can break down into smaller, simpler subproblems, and so on. DP problem usually reuse solutions to lower level subproblems.
We define dp[m+1][n+1]
represents a set of longest common subsequect of prefiex Xi and Yj. given that:
dp[0][0] = 0;
if X[i - 1]==Y[j -1] (current character is the same), dp[i][j] = 1 + d[i - 1][j - 1]
if X[i-1]!=Y[j-1], dp[i][j] = max(dp[i-1],j],dp[i][j-1])
repeat until computed the whole string, dp[m][n] is the answer.
For example:
Complexity Analysis
Time Complexity: O(N*M)
Space Complexity: O(N*M)
N - the length of text1
M - the length of text2
Code
Reference
Last updated