-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
32 lines (26 loc) · 924 Bytes
/
LongestCommonSubsequence.java
File metadata and controls
32 lines (26 loc) · 924 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
26
27
28
29
30
31
32
//Standard problem : This JAVA code gets the length of the longest common subsequence following the dynamic programming approach.
class Solution {
Integer[][] dp;
public int longestCommonSubsequence(String text1, String text2) {
char[] x= text1.toCharArray();
char[] y= text2.toCharArray();
int xi = x.length -1;
int yi = y.length -1;
dp = new Integer[xi +1][yi +1];
return helper(x, y, xi, yi);
}
private int helper(char[] x, char[] y, int xi, int yi){
if(xi <0 || yi < 0){
return 0;
}
if(dp[xi][yi] != null){
return dp[xi][yi];
}
if(x[xi] == y[yi]){
dp[xi][yi] =1+ helper(x, y, xi-1, yi -1);
}
else
dp[xi][yi] =Math.max(helper(x, y, xi-1, yi), helper(x, y, xi, yi -1));
return dp[xi][yi];
}
}