-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlcs_memoized.java
48 lines (28 loc) · 1.18 KB
/
lcs_memoized.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Arrays;
public class lcs_memoized {
public int longestCommonSubsequence(String text1, String text2) {
int n = text1.length();
int m = text2.length();
int[][] t = new int[1001][1001]; // matrix initialized with variables changing in recursion
for(int[] row : t){ // default set with -1
Arrays.fill(row , -1);
}
return helper(text1 , text2 , n , m , t);
}
public static int helper( String s1 , String s2 , int n , int m,int[][] dp) {
if(n == 0 || m == 0){
return 0;
}
if(dp[n][m] != -1){
return dp[n][m]; // if value already present return rather than again going in recursion
}
if(s1.charAt(n-1) == s2.charAt(m - 1)){
// storing for further use
return dp[n][m] = 1 + helper(s1, s2 , n-1 , m - 1,dp);
}
else{
// storing for further use
return dp[n][m] = Math.max((helper(s1,s2,n,m-1,dp)) , (helper(s1,s2,n-1,m,dp)));
}
}
}