Formatted question description: https://leetcode.ca/all/1044.html

1044. Longest Duplicate Substring (Hard)

Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.)

Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is "".)

 

Example 1:

Input: "banana"
Output: "ana"

Example 2:

Input: "abcd"
Output: ""

 

Note:

  1. 2 <= S.length <= 10^5
  2. S consists of lowercase English letters.

Related Topics:
Hash Table, Binary Search

Solution 1. Binary Search + Rabin Karp

// OJ: https://leetcode.com/problems/longest-duplicate-substring/
// Time: average O(NlogN), worst O(N^2 * logN)
// Space: O(N)
class Solution {
    int findDup(string &s, int len) {
        unordered_map<unsigned, vector<int>> m;
        unsigned h = 0, p = 1, d = 16777619;
        for (int i = 0; i < s.size(); ++i) {
            h = h * d + s[i] - 'a';
            if (i >= len) h -= (s[i - len] - 'a') * p;
            else p *= d;
            if (i >= len - 1) {
                if (m.count(h)) {
                    for (int k : m[h]) {
                        int j = 0;
                        for (; j < len && s[k + j] == s[i - len + 1 + j]; ++j);
                        if (j == len) return k;
                    } 
                }
                m[h].push_back(i - len + 1);
            }
        }
        return -1;
    }
public:
    string longestDupSubstring(string S) {
        int L = 1, R = S.size(), start = 0;
        while (L <= R) {
            int M = (L + R) / 2, i = findDup(S, M);
            if (i != -1) {
                L = M + 1;
                start = i;
            } else R = M - 1;
        }
        return S.substr(start, R);
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/longest-duplicate-substring/
// Time: O(N^2 * logN)
// Space: O(N)
// Ref: https://leetcode.com/problems/longest-duplicate-substring/discuss/695101/C%2B%2B-short-O(n-log(n))-solution-with-std%3A%3Aunordered_setlessstd%3A%3Astring_viewgreater
class Solution {
public:
    string longestDupSubstring(string S) {
        int N = S.size(), L = 0, R = N - 1;
        string_view ans;
        while (L <= R) {
            unordered_set<string_view> s;
            int M = (L + R) / 2;
            bool found = false;
            for (int i = 0; i <= N - M; ++i) {
                const auto [it, inserted] = s.emplace(S.data() + i, M);
                if (!inserted) {
                    found = true;
                    ans = *it;
                    break;
                }
            }
            if (found) L = M + 1;
            else R = M - 1;
        }
        return {ans.begin(), ans.end()};
    }
};

Java

  • class Solution {
        public String longestDupSubstring(String S) {
            int length = S.length();
            int[] nums = new int[length];
            for (int i = 0; i < length; i++)
                nums[i] = S.charAt(i) - 'a';
            int base = 26;
            final long MODULO = (long) Math.pow(2, 32);
            int low = 1, high = length;
            while (low < high) {
                int subLength = (high - low) / 2 + low;
                if (search(subLength, base, MODULO, length, nums) >= 0)
                    low = subLength + 1;
                else
                    high = subLength;
            }
            int start = search(low - 1, base, MODULO, length, nums);
            if (start >= 0)
                return S.substring(start, start + low - 1);
            else
                return "";
        }
    
        public int search(int subLength, int base, final long MODULO, int length, int[] nums) {
            long hash = 0;
            for (int i = 0; i < subLength; i++)
                hash = (hash * base + nums[i]) % MODULO;
            Set<Long> set = new HashSet<Long>();
            set.add(hash);
            long baseLong = 1;
            for (int i = 0; i < subLength; i++)
                baseLong = (baseLong * base) % MODULO;
            int maxIndex = length - subLength;
            for (int i = 1; i <= maxIndex; i++) {
                hash = (hash * base - nums[i - 1] * baseLong % MODULO + MODULO) % MODULO;
                hash = (hash + nums[i + subLength - 1]) % MODULO;
                if (set.contains(hash))
                    return i;
                set.add(hash);
            }
            return -1;
        }
    }
    
  • // OJ: https://leetcode.com/problems/longest-duplicate-substring/
    // Time: O(NlogN)
    // Space: O(N)
    class Solution {
        int findDup(string &s, int len) {
            unordered_set<unsigned long long> st;
            unsigned long long d = 16777619, h = 0, p = 1;
            for (int i = 0; i < s.size(); ++i) {
                h = h * d + s[i];
                if (i < len) p *= d;
                else h -= s[i - len] * p;
                if (i >= len - 1) {
                    if (st.count(h)) return i - len + 1;
                    st.insert(h);
                }
            }
            return -1;
        }
    public:
        string longestDupSubstring(string s) {
            int L = 0, R = s.size() - 1, start = 0;
            while (L < R) {
                int M = (L + R + 1) / 2, i = findDup(s, M);
                if (i != -1) {
                    L = M;
                    start = i;
                } else R = M - 1;
            }
            return s.substr(start, L);
        }
    };
    
  • print("Todo!")
    

All Problems

All Solutions