package com.opendoorlogistics.codefromweb; /** * From http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring#Java * @author Phil * */ public class LongestCommonSubstring { 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 + 1][sl + 1]; for (int s = 0; s <= sl; s++) table[0][s] = 0; for (int f = 0; f <= fl; f++) table[f][0] = 0; for (int i = 1; i <= fl; i++) { for (int j = 1; j <= sl; j++) { if (first.charAt(i - 1) == second.charAt(j - 1)) { if (i == 1 || j == 1) { 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; } }