forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubstring.java
More file actions
37 lines (33 loc) · 1.03 KB
/
LongestCommonSubstring.java
File metadata and controls
37 lines (33 loc) · 1.03 KB
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
public class LongestCommonSubSequence
{
static int LCSubStr(char X[], char Y[], int m, int n)
{
int LCStuff[][] = new int[m + 1][n + 1];
int result = 0;
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
LCStuff[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
{
LCStuff[i][j] = LCStuff[i - 1][j - 1] + 1;
result = Integer.max(result, LCStuff[i][j]);
}
else
LCStuff[i][j] = 0;
}
}
return result;
}
public static void main(String[] args)
{
String X = "abdcabbddacd";
String Y = "bdacd";
int m = X.length();
int n = Y.length();
System.out.println("Length of Longest Common Substring is "
+ LCSubStr(X.toCharArray(), Y.toCharArray(), m, n));
}
}