-
Notifications
You must be signed in to change notification settings - Fork 93
/
LongestCommonSubstring.java
58 lines (44 loc) · 1.37 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
package questions.virendra;
import java.util.ArrayList;
import java.util.List;
public class LongestCommonSubstring {
public static List<String> commonSubstring(String S1, String S2)
{
Integer match[][] = new Integer[S1.length()][S2.length()];
int len1 = S1.length();
int len2 = S2.length();
int max = Integer.MIN_VALUE; //Maximum length of the string
ArrayList<String> result = null; //Result list
for(int i=0; i<len1; i++)
{
for(int j=0; j<len2; j++)
{
if(S1.charAt(i) == S2.charAt(j))
{
if ( i == 0 || j==0) match[i][j] = 1;
else match[i][j] = match[i-1][j-1] + 1;
if(match[i][j] > max) //If you find a longer common substring re-initialize the max count and update the result list.
{
max = match[i][j];
result = new ArrayList<String>();
result.add(S1.substring(i-max+1, i+1)); //substring starts at i-max+1 and ends at i
}
else if(match[i][j] == max) // else if you find a common substring with the max length, store it in the list.
{
result.add(S1.substring(i-max+1, i+1));
}
}
else match[i][j] = 0;
}
}
return result;
}
public static void main(String args[])
{
List<String> result = commonSubstring("CLCL", "LCLC");
for(String str: result)
{
System.out.println(str);
}
}
}