![]() |
VOOZH | about |
A repeating substring is a substring that occurs in two (or more) locations in the string. Your task is to find the longest repeating substring in a given string.
Example:
Input: s = "cabababc"
Output: ababInput: s = "babababb"
Output: babab
Approach:
The solution is based on two main concepts: Suffix Arrays and Longest Common Prefix (LCP) Arrays.
Suffix Arrays: A suffix array is a sorted array of all suffixes of a given string. The suffixes are sorted in lexicographical order. The purpose of creating a suffix array is to sort all suffixes so that we can search for patterns (in this case, repeating substrings) in the sorted list of suffixes.
Longest Common Prefix (LCP) Array: The LCP array is an array that stores the longest common prefix between two consecutive suffixes in the sorted suffix array. The purpose of creating an LCP array is to find the longest common prefix between all pairs of consecutive suffixes. This helps in finding the longest repeating substring.
The core logic of the solution is as follows:
- Step 1: Build the suffix array of the string. This is done using the Manber-Myers algorithm, which is an efficient algorithm to build a suffix array in O(n log n) time. The algorithm starts by sorting all 1-length suffixes, then 2-length, 4-length, and so on until all suffixes are sorted.
- Step 2: Once the suffix array is built, the next step is to build the LCP array. This is done by comparing characters of suffixes one by one. If the characters match, increment the count of the longest common prefix.
- Step 3: After the LCP array is built, the maximum value in the LCP array is the length of the longest repeating substring. The substring itself can be obtained from the suffix array.
- Step 4: If the maximum LCP is 0, it means there are no repeating substrings, so the program outputs -1. Otherwise, it prints the longest repeating substring.
Step-by-step approach:
Below are the implementation of the above approach:
abab
Time Complexity: O(n log n)
Auxiliary Space: O(n)