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.
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.