Longest Duplicate Substring

HardStringHash TableBinary Search

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:

The string 'abcd' has all unique characters with no repeating substrings, so the result is an empty string.

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

"abc" repeats at positions 0-2 and 3-5, and no longer duplicate substring exists.

Constraints

  • 2 ≤ s.length ≤ 3 * 10⁴

Ready to solve this problem?

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