Longest Duplicate Substring - Problem
Find the Longest Duplicate Substring
You're given a string
Goal: Return any duplicate substring with the maximum possible length. If no duplicates exist, return an empty string
Example: In the string
This problem tests your understanding of string processing, hashing techniques, and binary search optimization.
You're given a string
s and need to find the longest substring that appears at least twice in the string. The duplicate occurrences can overlap with each other.Goal: Return any duplicate substring with the maximum possible length. If no duplicates exist, return an empty string
"".Example: In the string
"banana", both "an" and "ana" appear twice, but "ana" is longer, so we return "ana".This problem tests your understanding of string processing, hashing techniques, and binary search optimization.
Input & Output
example_1.py โ Basic Duplicate
$
Input:
s = "banana"
โบ
Output:
"ana"
๐ก Note:
Both "an" and "ana" appear twice in "banana", but "ana" is longer so we return it. The duplicates are at positions (1-3) and (3-5).
example_2.py โ No Duplicates
$
Input:
s = "abcd"
โบ
Output:
""
๐ก Note:
All characters are unique, so no substring appears more than once. Return empty string.
example_3.py โ Overlapping Duplicates
$
Input:
s = "aa"
โบ
Output:
"a"
๐ก Note:
The character "a" appears twice (overlapping is allowed). This is the longest possible duplicate.
Constraints
- 2 โค s.length โค 3 ร 104
- s consists of lowercase English letters only
- Time limit: Solution must run efficiently for maximum constraints
Visualization
Tap to expand
Understanding the Visualization
1
Set Search Range
Binary search between minimum (0) and maximum possible duplicate length (n-1)
2
Test Middle Length
For the middle length, use rolling hash to check all substrings of that length
3
Rolling Hash Magic
Calculate hash in O(1) time for each new substring by removing leftmost and adding rightmost character
4
Duplicate Detection
If same hash appears twice, verify it's a real match (not just hash collision)
5
Adjust Search
If duplicates found, try longer lengths; otherwise try shorter lengths
Key Takeaway
๐ฏ Key Insight: Binary search narrows down the optimal length efficiently, while rolling hash avoids expensive string comparisons by computing hashes in constant time.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code