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.
1function longestDupSubstring(s) {
2 const MOD = 10000007;
3 const BASE = 26;
4
5 function search(len) {
6 let hash = 0,
7 baseL = 1;
8 const seen = new Set();
9
10 for (let i = 0; i < len; ++i) {
11 hash = (hash * BASE + (s.charCodeAt(i) - 'a'.charCodeAt(0))) % MOD;
12 baseL = (i < len - 1) ? (baseL * BASE) % MOD : baseL;
13 }
14 seen.add(hash);
15 for (let i = len; i < s.length; ++i) {
16 hash = (hash * BASE - (s.charCodeAt(i - len) - 'a'.charCodeAt(0)) * baseL % MOD + MOD) % MOD;
17 hash = (hash + (s.charCodeAt(i) - 'a'.charCodeAt(0))) % MOD;
18 if (seen.has(hash)) return s.substring(i - len + 1, i + 1);
19 seen.add(hash);
20 }
21 return "";
22 }
23
24 let left = 0,
25 right = s.length - 1,
26 result = "";
27
28 while (left <= right) {
29 const mid = left + ((right - left) >> 1);
30 const dup = search(mid);
31 if (dup) {
32 left = mid + 1;
33 result = dup;
34 } else {
35 right = mid - 1;
36 }
37 }
38 return result;
39}
40
41console.log(longestDupSubstring("banana"));
42
This JavaScript implementation makes use of binary search and a custom hash function to find duplicate substrings. A set is used to manage and compare hash values efficiently for potential duplicates.
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.
1class Solution:
2 def longestDupSubstring(self, s: str) -> str:
3 suffixes = [s[i:] for i in range(len(s))]
4 suffixes.sort()
5
6 def common_prefix_length(s1, s2):
7 min_len = min(len(s1), len(s2))
8 for i in range(min_len):
9 if s1[i] != s2[i]:
10 return i
11 return min_len
12
13 result = ""
14 for i in range(1, len(s)):
15 len_common = common_prefix_length(suffixes[i - 1], suffixes[i])
16 if len_common > len(result):
17 result = suffixes[i][:len_common]
18
19 return result
20
21# Example usage:
22s = "banana"
23solution = Solution()
24print(solution.longestDupSubstring(s))
This Python solution generates and sorts the suffix array, then finds the longest common prefix of each consecutive suffix by comparing characters, revealing the longest repeated substring.