Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5d0639

Browse files
Create Printing_longest_common_substring.java
1 parent 40adb12 commit f5d0639

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//To print the Longest common Substring
2+
3+
public class LongestCommonSubstring{
4+
5+
public static void main(String []args){
6+
String a = "abcdxyz";
7+
String b = "xyzabcd";
8+
int i,j;
9+
int lena = a.length();
10+
int lenb = b.length();
11+
int[][] arr = new int[lena+ 1][lenb+1];
12+
13+
for(i = 0; i<= lena; i++ )
14+
{
15+
for(j = 0;j<=lenb; j++)
16+
{
17+
if(i==0 || j==0)
18+
arr[i][j] = 0;
19+
}
20+
}
21+
for(i = 1; i<= lena; i++ )
22+
{
23+
for(j = 1;j<=lenb; j++)
24+
{
25+
if(a.charAt(i-1) == b.charAt(j-1))
26+
{
27+
arr[i][j] = 1 + arr[i-1][j-1];
28+
}
29+
else
30+
arr[i][j] = 0;
31+
}
32+
}
33+
int max =0;
34+
int q = 3;
35+
for(i = 0; i<= lena; i++ )
36+
{
37+
for(j = 0;j<=lenb; j++)
38+
{
39+
40+
if(arr[i][j]>max)
41+
{
42+
max = arr[i][j];
43+
q = i;
44+
}
45+
}
46+
}
47+
48+
//Pringint the subString
49+
String p = "";
50+
char temp;
51+
for (int k = q-max ; k < q; k++)
52+
{
53+
temp = a.charAt(k);
54+
p = p+temp;
55+
//System.out.print(a.charAt(k));
56+
}
57+
/*
58+
for(i = 0; i<= lena; i++ )
59+
{
60+
for(j = 0;j<=lenb; j++)
61+
{
62+
63+
System.out.print(arr[i][j]+" ");
64+
}
65+
System.out.println("");
66+
}
67+
*/
68+
System.out.println("Longest Common Substring : "+max+" length and Substring is " + p);
69+
}
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /