forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCSThreeStrings.java
67 lines (48 loc) · 1.49 KB
/
LCSThreeStrings.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
To find the Longest Common Subsequence of three
strings using Bottom Up approach.
*/
import java.util.Scanner;
public class LCSThreeStrings {
private static void longestCommonSubsequeneceBottomUp(String a, String b, String c) {
int[][][] dp = new int[a.length() + 1][b.length() + 1][c.length() + 1];
// for loop will start from last index of each string
// in case any of the three strings is empty then
// answer will be 0.
for (int i = a.length() - 1; i >= 0; i--) {
for (int j = b.length() - 1; j >= 0; j--) {
for (int k = c.length() - 1; k >= 0; k--) {
if (a.charAt(i) == b.charAt(j) && a.charAt(i) == c.charAt(k)) {
dp[i][j][k] = 1 + dp[i + 1][j + 1][k + 1];
} else {
int x = dp[i + 1][j][k];
int y = dp[i][j + 1][k];
int z = dp[i][j][k + 1];
dp[i][j][k] = Math.max(x, Math.max(y, z));
}
}
}
}
System.out.println("Length of Longest Common Subsequence - " + dp[0][0][0]);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print ("Enter String A - ");
String a = sc.next();
System.out.print ("Enter String B - ");
String b = sc.next();
System.out.print ("Enter String C - ");
String c = sc.next();
longestCommonSubsequeneceBottomUp(a, b, c);
}
}
/**
Time Complexity : O(A*B*C)
Space Complexity : O(A*B*C)
Input:
Enter String A - adjkudg
Enter String B - bsdkdefg
Enter String C - dkelfudr
Output:
Length of Longest Common Subsequence - 3
*/