@Test public void testLongestRepeatedSubstring() { Assert.assertEquals("ana", longestRepeatedSubstring("banana")); Assert.assertEquals( " can do for you", longestRepeatedSubstring("Ask not what your country can do for you, ask what you can do for your country")); }
public static String longestRepeatedSubstring(String str) { final String[] suffixArray = buildSuffixArray(str); Arrays.sort(suffixArray); String temp = ""; String lrs = ""; String aSuffix = null; String anotherSuffix = null; int len = -1; for (int i = 0; i < suffixArray.length - 1; ++i) { aSuffix = suffixArray[i]; anotherSuffix = suffixArray[i + 1]; if (aSuffix.trim().isEmpty() || anotherSuffix.trim().isEmpty()) { continue; } else if (anotherSuffix.startsWith(aSuffix)) { LOGGER.debug("\"{}\" starts with \"{}\".", anotherSuffix, aSuffix); temp = aSuffix; } else { len = Math.min(aSuffix.length(), anotherSuffix.length()); int j = 1; while (j <= len && aSuffix.regionMatches(0, anotherSuffix, 0, j)) { LOGGER.debug("Comparing \"{}\" and \"{}\".", aSuffix.substring(0, j), anotherSuffix.substring(0, j)); ++j; } temp = aSuffix.substring(0, --j); } lrs = temp.length() > lrs.length() ? temp : lrs; } return lrs; }
Algorithms { public static String longestRepeatedSubstring(String str) { final String[] suffixArray = buildSuffixArray(str); Arrays.sort(suffixArray); String temp = ""; String lrs = ""; String aSuffix = null; String anotherSuffix = null; int len = -1; for (int i = 0; i < suffixArray.length - 1; ++i) { aSuffix = suffixArray[i]; anotherSuffix = suffixArray[i + 1]; if (aSuffix.trim().isEmpty() || anotherSuffix.trim().isEmpty()) { continue; } else if (anotherSuffix.startsWith(aSuffix)) { LOGGER.debug("\"{}\" starts with \"{}\".", anotherSuffix, aSuffix); temp = aSuffix; } else { len = Math.min(aSuffix.length(), anotherSuffix.length()); int j = 1; while (j <= len && aSuffix.regionMatches(0, anotherSuffix, 0, j)) { LOGGER.debug("Comparing \"{}\" and \"{}\".", aSuffix.substring(0, j), anotherSuffix.substring(0, j)); ++j; } temp = aSuffix.substring(0, --j); } lrs = temp.length() > lrs.length() ? temp : lrs; } return lrs; } }
Algorithms { public static String longestRepeatedSubstring(String str) { final String[] suffixArray = buildSuffixArray(str); Arrays.sort(suffixArray); String temp = ""; String lrs = ""; String aSuffix = null; String anotherSuffix = null; int len = -1; for (int i = 0; i < suffixArray.length - 1; ++i) { aSuffix = suffixArray[i]; anotherSuffix = suffixArray[i + 1]; if (aSuffix.trim().isEmpty() || anotherSuffix.trim().isEmpty()) { continue; } else if (anotherSuffix.startsWith(aSuffix)) { LOGGER.debug("\"{}\" starts with \"{}\".", anotherSuffix, aSuffix); temp = aSuffix; } else { len = Math.min(aSuffix.length(), anotherSuffix.length()); int j = 1; while (j <= len && aSuffix.regionMatches(0, anotherSuffix, 0, j)) { LOGGER.debug("Comparing \"{}\" and \"{}\".", aSuffix.substring(0, j), anotherSuffix.substring(0, j)); ++j; } temp = aSuffix.substring(0, --j); } lrs = temp.length() > lrs.length() ? temp : lrs; } return lrs; }  }
Algorithms { public static String longestRepeatedSubstring(String str) { final String[] suffixArray = buildSuffixArray(str); Arrays.sort(suffixArray); String temp = ""; String lrs = ""; String aSuffix = null; String anotherSuffix = null; int len = -1; for (int i = 0; i < suffixArray.length - 1; ++i) { aSuffix = suffixArray[i]; anotherSuffix = suffixArray[i + 1]; if (aSuffix.trim().isEmpty() || anotherSuffix.trim().isEmpty()) { continue; } else if (anotherSuffix.startsWith(aSuffix)) { LOGGER.debug("\"{}\" starts with \"{}\".", anotherSuffix, aSuffix); temp = aSuffix; } else { len = Math.min(aSuffix.length(), anotherSuffix.length()); int j = 1; while (j <= len && aSuffix.regionMatches(0, anotherSuffix, 0, j)) { LOGGER.debug("Comparing \"{}\" and \"{}\".", aSuffix.substring(0, j), anotherSuffix.substring(0, j)); ++j; } temp = aSuffix.substring(0, --j); } lrs = temp.length() > lrs.length() ? temp : lrs; } return lrs; }  static String longestRepeatedSubstring(String str); }
Algorithms { public static String longestRepeatedSubstring(String str) { final String[] suffixArray = buildSuffixArray(str); Arrays.sort(suffixArray); String temp = ""; String lrs = ""; String aSuffix = null; String anotherSuffix = null; int len = -1; for (int i = 0; i < suffixArray.length - 1; ++i) { aSuffix = suffixArray[i]; anotherSuffix = suffixArray[i + 1]; if (aSuffix.trim().isEmpty() || anotherSuffix.trim().isEmpty()) { continue; } else if (anotherSuffix.startsWith(aSuffix)) { LOGGER.debug("\"{}\" starts with \"{}\".", anotherSuffix, aSuffix); temp = aSuffix; } else { len = Math.min(aSuffix.length(), anotherSuffix.length()); int j = 1; while (j <= len && aSuffix.regionMatches(0, anotherSuffix, 0, j)) { LOGGER.debug("Comparing \"{}\" and \"{}\".", aSuffix.substring(0, j), anotherSuffix.substring(0, j)); ++j; } temp = aSuffix.substring(0, --j); } lrs = temp.length() > lrs.length() ? temp : lrs; } return lrs; }  static String longestRepeatedSubstring(String str); static final Logger LOGGER; }