-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlongest _common_substring.java
91 lines (73 loc) · 1.69 KB
/
longest _common_substring.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
84
85
86
87
88
89
90
91
/*
Given two strings ‘X’ and ‘Y’, find the length of the longest common substring.
Examples :
Input : X = "GeeksforGeeks", y = "GeeksQuiz"
Output : 5
The longest common substring is "Geeks" and is of
length 5.
Input : X = "abcdxyz", y = "xyzabcd"
Output : 4
The longest common substring is "abcd" and is of
length 4.
Input : X = "zxabcdezy", y = "yzabcdezx"
Output : 6
The longest common substring is "abcdez" and is of
length 6.
Input:
First line of the input contains no of test cases T,the T test cases follow.
Each test case consist of 2 space separated integers A and B denoting the size of string X and Y respectively
The next two lines contains the 2 string X and Y.
Output:
For each test case print the length of longest common substring of the two strings .
Constraints:
1<=T<=200
1<=size(X),size(Y)<=100
Example:
Input:
2
6 6
ABCDGH
ACDGHR
3 2
ABC
AC
Output:
4
1
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc= new Scanner(System.in);
int t= sc.nextInt();
while(t-->0)
{
int m=sc.nextInt();
int n=sc.nextInt();
String str1=sc.next();
String str2=sc.next();
int dp[][]= new int[str1.length()+1][str2.length()+1];
int max=0;
for(int i=0;i<=str1.length();i++)
{
for(int j=0;j<=str2.length();j++)
{
if(i==0 ||j==0)
dp[i][j]=0;
else if(str1.charAt(i-1)==str2.charAt(j-1))
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
dp[i][j]=0;
if(dp[i][j]>max)
max=dp[i][j];
}
}
System.out.println(max);
}
}
}