-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLCS-of-three-strings.java
More file actions
31 lines (28 loc) · 957 Bytes
/
LCS-of-three-strings.java
File metadata and controls
31 lines (28 loc) · 957 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
//User function Template for Java
class Solution
{
int LCSof3(String A, String B, String C, int n1, int n2, int n3)
{
// code here
int[][][] dp = new int[n1+1][n2+1][n3+1];
for (int i=0; i<=n1; i++)
{
for (int j=0; j<=n2; j++)
{
for (int k=0; k<=n3; k++)
{
if (i == 0 || j == 0||k==0)
dp[i][j][k] = 0;
else if (A.charAt(i - 1) == B.charAt(j - 1)
&& A.charAt(i - 1)==C.charAt(k - 1))
dp[i][j][k] = dp[i-1][j-1][k-1] + 1;
else
dp[i][j][k] = Math.max(Math.max(dp[i-1][j][k],
dp[i][j-1][k]),
dp[i][j][k-1]);
}
}
}
return dp[n1][n2][n3];
}
}