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.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 static int MOD = 10000007;
6 static int BASE = 26;
7
8 public string LongestDupSubstring(string s) {
9 int n = s.Length;
10 int left = 1, right = n - 1;
11 string result = "";
12
13 while (left <= right) {
14 int mid = left + (right - left) / 2;
15 string dup = FindDup(s, mid);
16 if (dup.Length > 0) {
17 left = mid + 1;
18 result = dup;
19 } else {
20 right = mid - 1;
21 }
22 }
23 return result;
24 }
25
26 private string FindDup(string s, int len) {
27 var seen = new HashSet<int>();
28 long hash = 0, power = 1;
29 for (int i = 0; i < len; i++) {
30 hash = (hash * BASE + (s[i] - 'a')) % MOD;
31 if (i < len - 1) power = (power * BASE) % MOD;
32 }
33 seen.Add((int) hash);
34 for (int i = len; i < s.Length; i++) {
35 hash = (hash * BASE - power * (s[i - len] - 'a') % MOD + MOD) % MOD;
36 hash = (hash + (s[i] - 'a')) % MOD;
37 if (seen.Contains((int)hash)) {
38 return s.Substring(i - len + 1, len);
39 }
40 seen.Add((int) hash);
41 }
42 return "";
43 }
44
45 public static void Main() {
46 Solution solution = new Solution();
47 Console.WriteLine(solution.LongestDupSubstring("banana"));
48 }
49}
50
Using C#, this program applies binary search for determining the maximum possible length of a duplicate substring. It utilizes HashSet
for creating a rolling hash to check for duplicate substrings efficiently.
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.
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public string LongestDupSubstring(string s) {
6 int n = s.Length;
7 var suffixes = new List<string>();
8 for (int i = 0; i < n; i++) {
9 suffixes.Add(s.Substring(i));
10 }
11 suffixes.Sort();
12
13 string result = "";
14 for (int i = 1; i < n; i++) {
15 int len = CommonPrefixLength(suffixes[i - 1], suffixes[i]);
16 if (len > result.Length) {
17 result = suffixes[i].Substring(0, len);
18 }
19 }
20 return result;
21 }
22
23 private int CommonPrefixLength(string s1, string s2) {
24 int length = 0;
25 while (length < s1.Length && length < s2.Length && s1[length] == s2[length]) {
26 length++;
27 }
28 return length;
29 }
30
31 public static void Main() {
32 Solution solution = new Solution();
33 Console.WriteLine(solution.LongestDupSubstring("banana"));
34 }
35}
36
This C# implementation constructs a suffix array, sorts it, and evaluates longest common prefixes between sorted suffixes to discern the longest duplicate substring.