Professional Documents
Culture Documents
Longest Common SubString
Longest Common SubString
String str2="Sohail is a good student and a boy. He is a good student and a girlfriend at Denver";
if (str1==null || str2==null)
return 0;
int maxlen = 0;
int lastSubsBegin = 0;
if (str1.charAt(i) != str2.charAt(j))
num[i][j] = 0;
else
if ((i == 0) || (j == 0))
num[i][j] = 1;
1
Longest Common Substring Algorithm O(mn)
else
maxlen = num[i][j];
if (lastSubsBegin == thisSubsBegin)
{//if the current LCS is the same as the last time this block ran
sequenceBuilder.append(str1.charAt(i));
else
lastSubsBegin = thisSubsBegin;
sequenceBuilder.setLength(0); //clear it
sequenceBuilder.append(str1.substring(lastSubsBegin, (i + 1) ));
sequence = sequenceBuilder.toString();
return maxlen;