forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestCommonSubString.java
83 lines (75 loc) · 2.32 KB
/
longestCommonSubString.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
The longest common suffix has following optimal substructure property
LCSubstring(arr1, arr2, m, n) = LCSubstring(arr1,arr2, m-1, n-1) + 1 if X[m-1] = Y[n-1]
0 Otherwise (if X[m-1] != Y[n-1])
The maximum length Longest Common Suffix is the longest common substring.
LCSubstring(arr1, arr2, m, n) = LCSubstring(arr1, arr2, i, j) where 1 <= i <= m
and 1 <= j <= n
*/
import java.util.Arrays;
class longestCommonSubString
{
public static void longestCommonSubString_util(char[] arr1, char[] arr2, int m, int n)
{
int[][] lcsubstring = new int[m+1][n+1];
int max = 0;
// p and q storing the index in lcsubstring for later printing
int p = 0;
int q = 0;
for(int i=1; i <=m; i++)
{
for(int j=1; j<=n; j++)
{
if(arr1[i-1] == arr2[j-1])
{
lcsubstring[i][j] = 1 + lcsubstring[i-1][j-1];
if(lcsubstring[i][j] > max)
{
max = lcsubstring[i][j];
p = i;
q= j;
}
//max = Math.max(lcsubstring[i][j], max);
}
else
{
lcsubstring[i][j] = 0;
}
}
}
System.out.println("Length of longest Common SubString is : "+max);
//printing max substring we have to go diagonaly
int maxval = max;
char[] sub=new char[maxval+1];
sub[maxval]='\0';
int i=p;
int j=q;
while(i>0 && j>0)
{
if(arr1[i-1] == arr2[j-1])
{
sub[maxval-1]=arr1[i-1];
i--;
j--;
maxval--;
}
/*else if(lcsubstring[i-1][j] > lcsubstring[i][j-1])
{
i--;
}
else
{
j--;
}*/
}
System.out.print("The longest common substring is :"+ Arrays.toString(sub)); // ignore last char it is empty
}
public static void main(String[] args)
{
char[] arr1 = "abcdxyz".toCharArray();
char[] arr2 = "xyzabcd".toCharArray();
int m = arr1.length;
int n = arr2.length;
longestCommonSubString_util(arr1,arr2,m,n);
}
}