forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
25 lines (23 loc) · 944 Bytes
/
LongestCommonSubsequence.java
File metadata and controls
25 lines (23 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package dynamic;
public class LongestCommonSubsequence {
public int longestCommonSubsequenc(String text1,String text2){
if (text1 == null || text2 == null || text1.length() == 0
|| text2.length() == 0) return 0;
int m = text1.length(),n = text2.length();
int[][] dp = new int[m+1][n+1];
for (int i = 1; i <m+1 ; i++) {
for (int j = 1; j <=n ; j++) {
if (text1.charAt(i-1) == text2.charAt(j-1)) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
}
}
return dp[m][n];
}
public static void main(String[] args) {
String text1 = "abcdef";
String text2 = "acef";
LongestCommonSubsequence longestCommonSubsequence = new LongestCommonSubsequence();
int r = longestCommonSubsequence.longestCommonSubsequenc(text1,text2);
System.out.println(r);
}
}