From 14d5de5676467b6c267f7edbcdf491d6ed53acab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=87a=C4=9Flar=20Eker?= Date: Sun, 22 Feb 2026 16:23:48 +0300 Subject: [PATCH 1/2] Add Longest Repeated Substring algorithm Implement LongestRepeatedSubstring in the strings package using the existing SuffixArray and Kasai's algorithm for LCP array construction. Includes parameterized unit tests covering typical and edge cases. --- .../strings/LongestRepeatedSubstring.java | 83 +++++++++++++++++++ .../strings/LongestRepeatedSubstringTest.java | 46 ++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/LongestRepeatedSubstring.java create mode 100644 src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java diff --git a/src/main/java/com/thealgorithms/strings/LongestRepeatedSubstring.java b/src/main/java/com/thealgorithms/strings/LongestRepeatedSubstring.java new file mode 100644 index 000000000000..87c9278fd4bf --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/LongestRepeatedSubstring.java @@ -0,0 +1,83 @@ +package com.thealgorithms.strings; + +/** + * Finds the longest substring that occurs at least twice in a given string. + * + *

Uses the suffix array (via {@link SuffixArray}) and Kasai's algorithm + * to build the LCP (Longest Common Prefix) array, then returns the substring + * corresponding to the maximum LCP value.

+ * + *

Time complexity: O(n log² n) for suffix array construction + O(n) for LCP.

+ * + * @see Longest repeated substring problem + * @see SuffixArray + */ +public final class LongestRepeatedSubstring { + + private LongestRepeatedSubstring() { + } + + /** + * Returns the longest substring that appears at least twice in the given text. + * + * @param text the input string + * @return the longest repeated substring, or an empty string if none exists + */ + public static String longestRepeatedSubstring(String text) { + if (text == null || text.length() <= 1) { + return ""; + } + + final int[] suffixArray = SuffixArray.buildSuffixArray(text); + final int[] lcp = buildLcpArray(text, suffixArray); + + int maxLen = 0; + int maxIdx = 0; + for (int i = 0; i < lcp.length; i++) { + if (lcp[i] > maxLen) { + maxLen = lcp[i]; + maxIdx = suffixArray[i + 1]; + } + } + + return text.substring(maxIdx, maxIdx + maxLen); + } + + /** + * Builds the LCP (Longest Common Prefix) array using Kasai's algorithm. + * + *

LCP[i] is the length of the longest common prefix between the suffixes + * at positions suffixArray[i] and suffixArray[i+1] in sorted order.

+ * + * @param text the original string + * @param suffixArray the suffix array of the string + * @return the LCP array of length n-1 + */ + static int[] buildLcpArray(String text, int[] suffixArray) { + final int n = text.length(); + final int[] rank = new int[n]; + final int[] lcp = new int[n - 1]; + + for (int i = 0; i < n; i++) { + rank[suffixArray[i]] = i; + } + + int k = 0; + for (int i = 0; i < n; i++) { + if (rank[i] == n - 1) { + k = 0; + continue; + } + final int j = suffixArray[rank[i] + 1]; + while (i + k < n && j + k < n && text.charAt(i + k) == text.charAt(j + k)) { + k++; + } + lcp[rank[i]] = k; + if (k > 0) { + k--; + } + } + + return lcp; + } +} diff --git a/src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java b/src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java new file mode 100644 index 000000000000..6de890cde3c6 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java @@ -0,0 +1,46 @@ +package com.thealgorithms.strings; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class LongestRepeatedSubstringTest { + + @ParameterizedTest(name = "\"{0}\" -> \"{1}\"") + @MethodSource("provideTestCases") + void testLongestRepeatedSubstring(String input, String expected) { + assertEquals(expected, LongestRepeatedSubstring.longestRepeatedSubstring(input)); + } + + private static Stream provideTestCases() { + return Stream.of( + Arguments.of("banana", "ana"), + Arguments.of("abcabc", "abc"), + Arguments.of("aaaa", "aaa"), + Arguments.of("abcd", ""), + Arguments.of("a", ""), + Arguments.of("", ""), + Arguments.of(null, ""), + Arguments.of("aab", "a"), + Arguments.of("mississippi", "issi") + ); + } + + @ParameterizedTest(name = "\"{0}\" -> LCP={1}") + @MethodSource("provideLcpTestCases") + void testBuildLcpArray(String input, int[] expectedLcp) { + int[] suffixArray = SuffixArray.buildSuffixArray(input); + assertArrayEquals(expectedLcp, LongestRepeatedSubstring.buildLcpArray(input, suffixArray)); + } + + private static Stream provideLcpTestCases() { + return Stream.of( + Arguments.of("banana", new int[] {1, 3, 0, 0, 2}), + Arguments.of("ab", new int[] {0}) + ); + } +} From b442997f8e20c0ab00b63c5e78869b7b92b1579b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=CC=A7ag=CC=86lar=20Eker?= Date: Sun, 22 Feb 2026 16:57:29 +0300 Subject: [PATCH 2/2] style: reformat test file per clang-format and add coverage test --- .../strings/LongestRepeatedSubstringTest.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java b/src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java index 6de890cde3c6..366f6863340d 100644 --- a/src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java +++ b/src/test/java/com/thealgorithms/strings/LongestRepeatedSubstringTest.java @@ -17,17 +17,7 @@ void testLongestRepeatedSubstring(String input, String expected) { } private static Stream provideTestCases() { - return Stream.of( - Arguments.of("banana", "ana"), - Arguments.of("abcabc", "abc"), - Arguments.of("aaaa", "aaa"), - Arguments.of("abcd", ""), - Arguments.of("a", ""), - Arguments.of("", ""), - Arguments.of(null, ""), - Arguments.of("aab", "a"), - Arguments.of("mississippi", "issi") - ); + return Stream.of(Arguments.of("banana", "ana"), Arguments.of("abcabc", "abc"), Arguments.of("aaaa", "aaa"), Arguments.of("abcd", ""), Arguments.of("a", ""), Arguments.of("", ""), Arguments.of(null, ""), Arguments.of("aab", "a"), Arguments.of("aa", "a"), Arguments.of("mississippi", "issi")); } @ParameterizedTest(name = "\"{0}\" -> LCP={1}") @@ -38,9 +28,6 @@ void testBuildLcpArray(String input, int[] expectedLcp) { } private static Stream provideLcpTestCases() { - return Stream.of( - Arguments.of("banana", new int[] {1, 3, 0, 0, 2}), - Arguments.of("ab", new int[] {0}) - ); + return Stream.of(Arguments.of("banana", new int[] {1, 3, 0, 0, 2}), Arguments.of("ab", new int[] {0})); } }