Given two strings, find the longest common subsequence (LCS).
Your code should return the length of LCS.
What's the definition of Longest Common Subsequence?
For "ABCD"
and "EDCA"
, the LCS is "A"
(or "D"
, "C"
), return 1
.
For "ABCD"
and "EACB"
, the LCS is "AC"
, return 2
.
Solution 1. Recursion
If we start to compare two characters from the end of both strings, we'll have the following possible cases.
1. A.charAt(n1 - 1) == B.charAt(n2 - 1), then the lcs is 1 + lcs of (A[0.....n1 - 2], B[0....n2 - 2])
2. if they are not the same, then the lcs is the max of lcs of (A[0.....n1 - 1], B[0......n2 - 2]) and lcs of (A[0.....n1 - 2], B[0.....n2 - 1])
Both cases reduce the original problem to a smaller problem, which can be solved recursively.
The base case is when either the first or the second string has no more characters to be compared.
This solution does redundant work so it is not efficient. For example, say we have A[0....5] and B[0....5].
assume A[5] == B[5], then we proceed to solve lcs of (A[0....4], B[0....4]);
assume A[4] != B[4], then we need to solve lcs of (A[0....4], B[0....3]) and lcs of (A[0...3], B[0....4]);
For lcs A[0...4], B[0...3], we may need to solve lcs of A[0....3], B[0...3];
For lcs A[0...3], B[0...4], we may need to solve lcs of A[0....3], B[0...3] again;
And so on.
The bigger the two strings are, the more overlapping subproblems this recursive solution has to solve redundantly.
Naturally to avoid overlapping subproblems, we use dynamic programming.
1 public class Solution { 2 public int longestCommonSubsequence(String A, String B) { 3 if(A == null || B == null || A.length() == 0 || B.length() == 0){ 4 return 0; 5 } 6 return lcsRecursive(A, A.length() - 1, B, B.length() - 1); 7 } 8 private int lcsRecursive(String s1, int endIdx1, String s2, int endIdx2){ 9 if(endIdx1 < 0 || endIdx2 < 0){ 10 return 0; 11 } 12 if(s1.charAt(endIdx1) == s2.charAt(endIdx2)){ 13 return 1 + lcsRecursive(s1, endIdx1 - 1, s2, endIdx2 - 1); 14 } 15 return Math.max(lcsRecursive(s1, endIdx1, s2, endIdx2 - 1), lcsRecursive(s1, endIdx1 - 1, s2, endIdx2)); 16 } 17 }
Solution 2. Dynamic Programming, O(n^2) runtime, O(n^2) space
lcs[i][j]: the longest common subsequence between A[0.... i - 1] and B[0....j - 1]
State function is the same with the recursive formula in solution 1.
1 public class Solution { 2 public int longestCommonSubsequence(String A, String B) { 3 if(A == null || B == null || A.length() == 0 || B.length() == 0){ 4 return 0; 5 } 6 int[][] lcs = new int[A.length() + 1][B.length() + 1]; 7 for(int i = 0; i <= A.length(); i++){ 8 lcs[i][0] = 0; 9 } 10 for(int i = 0; i <= B.length(); i++){ 11 lcs[0][i] = 0; 12 } 13 for(int i = 1; i <= A.length(); i++){ 14 for(int j = 1; j <= B.length(); j++){ 15 if(A.charAt(i - 1) == B.charAt(j - 1)){ 16 lcs[i][j] = 1 + lcs[i - 1][j - 1]; 17 } 18 else{ 19 lcs[i][j] = Math.max(lcs[i - 1][j], lcs[i][j - 1]); 20 } 21 } 22 } 23 return lcs[A.length()][B.length()]; 24 } 25 }
Solution 3. Dynamic Programming, with optimized O(n) space
In solution 2, lcs[i][j] is only related to row i - 1 and i, so we can apply the rolling array techinque to
make the O(n^2) space usage to be O(n) space usage.
1 public class Solution { 2 public int longestCommonSubsequence(String A, String B) { 3 if(A == null || B == null || A.length() == 0 || B.length() == 0){ 4 return 0; 5 } 6 int[][] lcs = new int[2][B.length() + 1]; 7 for(int i = 0; i <= B.length(); i++){ 8 lcs[0][i] = 0; 9 } 10 for(int i = 1; i <= A.length(); i++){ 11 for(int j = 1; j <= B.length(); j++){ 12 if(A.charAt(i - 1) == B.charAt(j - 1)){ 13 lcs[i % 2][j] = 1 + lcs[(i - 1) % 2][j - 1]; 14 } 15 else{ 16 lcs[i % 2][j] = Math.max(lcs[(i - 1) % 2][j], lcs[i % 2][j - 1]); 17 } 18 } 19 } 20 return lcs[A.length() % 2][B.length()]; 21 } 22 }
Follow up question: Can you reconstruct one Longest Common Subsequence?
To do this, we must use the O(n^2) space dp solution as it keeps all the intermediate results that can be used to reconstruct one LCS.
1 public ArrayList<Character> reconstructLCS(String A, String B, int[][] lcs){ 2 ArrayList<Character> result = new ArrayList<Character>(); 3 int i = lcs.length - 1; 4 int j = lcs[0].length - 1; 5 while(i >= 1 && j >= 1){ 6 if(A.charAt(i - 1) == B.charAt(j - 1)){ 7 result.add(A.charAt(i - 1)); 8 i--; 9 j--; 10 } 11 else if(lcs[i - 1][j] > lcs[i][j - 1]){ 12 i--; 13 } 14 else{ 15 j--; 16 } 17 } 18 return result; 19 }
Related Problems
Longest Repeating Subsequence
Edit Distance
来源:https://www.cnblogs.com/lz87/p/7221335.html