Welcome to Subscribe On Youtube

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()};
    }
};
  • 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;
        }
    }
    
    ############
    
    class Solution {
        private long[] p;
        private long[] h;
    
        public String longestDupSubstring(String s) {
            int base = 131;
            int n = s.length();
            p = new long[n + 10];
            h = new long[n + 10];
            p[0] = 1;
            for (int i = 0; i < n; ++i) {
                p[i + 1] = p[i] * base;
                h[i + 1] = h[i] * base + s.charAt(i);
            }
            String ans = "";
            int left = 0, right = n;
            while (left < right) {
                int mid = (left + right + 1) >> 1;
                String t = check(s, mid);
                if (t.length() > 0) {
                    left = mid;
                    ans = t;
                } else {
                    right = mid - 1;
                }
            }
            return ans;
        }
    
        private String check(String s, int len) {
            int n = s.length();
            Set<Long> vis = new HashSet<>();
            for (int i = 1; i + len - 1 <= n; ++i) {
                int j = i + len - 1;
                long t = h[j] - h[i - 1] * p[j - i + 1];
                if (vis.contains(t)) {
                    return s.substring(i - 1, j);
                }
                vis.add(t);
            }
            return "";
        }
    }
    
  • // 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);
        }
    };
    
  • class Solution:
        def longestDupSubstring(self, s: str) -> str:
            def check(l):
                vis = set()
                for i in range(n - l + 1):
                    t = s[i : i + l]
                    if t in vis:
                        return t
                    vis.add(t)
                return ''
    
            n = len(s)
            left, right = 0, n
            ans = ''
            while left < right:
                mid = (left + right + 1) >> 1
                t = check(mid)
                ans = t or ans
                if t:
                    left = mid
                else:
                    right = mid - 1
            return ans
    
    
    
  • func longestDupSubstring(s string) string {
    	base, n := 131, len(s)
    	p := make([]int64, n+10)
    	h := make([]int64, n+10)
    	p[0] = 1
    	for i := 0; i < n; i++ {
    		p[i+1] = p[i] * int64(base)
    		h[i+1] = h[i]*int64(base) + int64(s[i])
    	}
    	check := func(l int) string {
    		vis := make(map[int64]bool)
    		for i := 1; i+l-1 <= n; i++ {
    			j := i + l - 1
    			t := h[j] - h[i-1]*p[j-i+1]
    			if vis[t] {
    				return s[i-1 : j]
    			}
    			vis[t] = true
    		}
    		return ""
    	}
    	left, right := 0, n
    	ans := ""
    	for left < right {
    		mid := (left + right + 1) >> 1
    		t := check(mid)
    		if len(t) > 0 {
    			left = mid
    			ans = t
    		} else {
    			right = mid - 1
    		}
    	}
    	return ans
    }
    

All Problems

All Solutions