Longest Duplicate Substring

Hard

Description

Given a string s, return any longest substring that occurs at least twice. Use binary search + Rabin-Karp rolling hash.

Examples

Input:s = "banana"
Output:"ana"
Explanation:

'ana' appears twice in 'banana'.

Input:s = "abcd"
Output:""
Explanation:

Edge case with empty result.

Input:s = "abcabcbb"
Output:"abcb"
Explanation:

The longest duplicate substring is 'abc', appearing at positions 0-2 and 3-5 in 'abcabcabc'. Longer substrings like 'abcabc' also repeat, making the actual longest duplicate substring 'abcabc' with length 6.

Constraints

  • 2 ≤ s.length ≤ 3 * 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!