This approach leverages binary search in conjunction with the Rabin-Karp (rolling hash) algorithm to find the longest duplicate substring within a given string.
We perform binary search on the length of the possible substring, starting from 1 to length of s
-1. For each mid-length obtained from the binary search, we use a rolling hash function to hash each substring of length mid
. This hash is used to quickly identify duplicates due to its constant time complexity for fixed-length substrings.
Time Complexity: O(n log n), where n is the length of the string. The binary search takes O(log n), and for each midpoint, hashing takes O(n).
Space Complexity: O(n), primarily for storing hash values and powers of base.
1class Solution:
2 def longestDupSubstring(self, s: str) -> str:
3 def search(length: int) -> str:
4 MOD = 10000007
5 BASE = 26
6 current_hash = 0
7 base_l = pow(BASE, length, MOD)
8 seen = set()
9 for i in range(length):
10 current_hash = (current_hash * BASE + ord(s[i]) - ord('a')) % MOD
11 seen.add(current_hash)
12 for i in range(length, len(s)):
13 current_hash = ((current_hash * BASE - (ord(s[i - length]) - ord('a')) * base_l) + ord(s[i]) - ord('a')) % MOD
14 if current_hash in seen:
15 return s[i - length + 1:i + 1]
16 seen.add(current_hash)
17 return ""
18
19 left, right = 1, len(s) - 1
20 result = ""
21 while left <= right:
22 mid = left + (right - left) // 2
23 dup = search(mid)
24 if dup:
25 left = mid + 1
26 result = dup
27 else:
28 right = mid - 1
29
30 return result
31
32# Example usage:
33s = "banana"
34solution = Solution()
35print(solution.longestDupSubstring(s))
This Python solution utilizes binary search to find the longest duplicate substring. It generates hashes for substrings and uses a set to detect duplicates efficiently, returning the longest one found.
This method involves constructing a suffix array from the input string and then performing binary search on the suffixes to find the longest duplicate substring.
Using suffix arrays, we can efficiently sort and group starting indices of the given string. Then, by employing binary search, we determine the largest-length substring that repeats. The Longest Common Prefix (LCP) array helps in assessing the similarity of suffixes at each binary search step.
Time Complexity: O(n^2 log n), primarily due to the sorting step where n is the length of the input string.
Space Complexity: O(n^2), largely for storing pointers to suffixes.
1#include <iostream>
2#include <vector>
3#include <string>
4#include <algorithm>
5using namespace std;
6
7string longestDupSubstring(string s) {
8 int n = s.size();
9 vector<string> suffixes(n);
10 for (int i = 0; i < n; i++) {
11 suffixes[i] = s.substr(i);
12 }
13 sort(suffixes.begin(), suffixes.end());
14
15 string result = "";
16 for (int i = 1; i < n; i++) {
17 string &s1 = suffixes[i - 1], &s2 = suffixes[i];
18 int length = 0, limit = min(s1.size(), s2.size());
19 while (length < limit && s1[length] == s2[length]) {
20 length++;
21 }
22 if (length > result.size()) {
23 result = s1.substr(0, length);
24 }
25 }
26 return result;
27}
28
29int main() {
30 cout << longestDupSubstring("banana") << endl;
31 return 0;
32}
33
This C++ code applies sorting to the suffixes to form a suffix array. Then it identifies the longest common prefix between consecutive entries to determine the longest duplicating substring efficiently.