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.
1#include <iostream>
2#include <unordered_set>
3#include <string>
4using namespace std;
5
6class Solution {
7 const int MOD = 10000007;
8 const int BASE = 26;
9public:
10 string longestDupSubstring(string s) {
11 int n = s.size();
12 int left = 1, right = n - 1;
13 string result = "";
14
15 while (left <= right) {
16 int mid = left + (right - left) / 2;
17 string dup = findDup(s, mid);
18 if (!dup.empty()) {
19 left = mid + 1;
20 result = dup;
21 } else {
22 right = mid - 1;
23 }
24 }
25 return result;
26 }
27
28 string findDup(string &s, int len) {
29 unordered_set<int> seen;
30 long long hash = 0, power = 1;
31 for (int i = 0; i < len; ++i) {
32 hash = (hash * BASE + (s[i] - 'a')) % MOD;
33 power = (power * BASE) % MOD;
34 }
35 seen.insert(hash);
36 for (int i = len; i < s.size(); ++i) {
37 hash = ((hash * BASE - (s[i - len] - 'a') * power % MOD + MOD) % MOD + (s[i] - 'a')) % MOD;
38 if (seen.count(hash)) {
39 return s.substr(i - len + 1, len);
40 }
41 seen.insert(hash);
42 }
43 return "";
44 }
45};
46
47int main() {
48 Solution sol;
49 string s = "banana";
50 cout << sol.longestDupSubstring(s) << endl;
51 return 0;
52}
53
This C++ solution follows similar logic to the C implementation but uses C++ containers like unordered_set
for efficiency in hash management. The binary search is used to determine the maximum length of duplicate substring, leveraging findDup
to extract the actual string.
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.
1import java.util.Arrays;
2
3public class Solution {
4 public String longestDupSubstring(String s) {
5 int n = s.length();
6 String[] suffixes = new String[n];
7 for (int i = 0; i < n; i++) {
8 suffixes[i] = s.substring(i);
9 }
10 Arrays.sort(suffixes);
11
12 String result = "";
13 for (int i = 1; i < n; i++) {
14 int len = commonPrefixLength(suffixes[i - 1], suffixes[i]);
15 if (len > result.length()) {
16 result = suffixes[i].substring(0, len);
17 }
18 }
19 return result;
20 }
21
22 private int commonPrefixLength(String s1, String s2) {
23 int len = 0, limit = Math.min(s1.length(), s2.length());
24 while (len < limit && s1.charAt(len) == s2.charAt(len)) {
25 len++;
26 }
27 return len;
28 }
29
30 public static void main(String[] args) {
31 Solution sol = new Solution();
32 System.out.println(sol.longestDupSubstring("banana"));
33 }
34}
35
In Java, this solution sorts suffixes to leverage the suffix array method. It utilizes commonPrefixLength
to find the longest repeat among consecutive suffixes.