Given two strings, find the longest common substring.
public static int longestSubstr(String first, String second) {
if (first == null || second == null || first.length() == 0 || second.length() == 0) {
return 0;
}
int maxLen = 0;
int fl = first.length();
int sl = second.length();
int[][] table = new int[fl][sl];
for (int i = 0; i < fl; i++) {
for (int j = 0; j < sl; j++) {
if (first.charAt(i) == second.charAt(j)) {
if (i == 0 || j == 0) {
table[i][j] = 1;
}
else {
table[i][j] = table[i - 1][j - 1] + 1;
}
if (table[i][j] > maxLen) {
maxLen = table[i][j];
}
}
}
}
return maxLen;
}
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks