Trains the technique from
LeetCode 1044Longest Duplicate SubstringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A device writes one long line of lowercase letters into its log, given to you as s. A stretch is any block of one or more letters that sit next to each other on that line.
A stretch is called echoed when it can be read off starting at two different positions of s. The two readings are permitted to overlap, so the line aaa has the echoed stretch aa.
Return the longest echoed stretch of s. Should several echoed stretches tie for the greatest length, return the one whose first appearance in s starts furthest to the left. If s has no echoed stretch, return the empty string "".
Example 1
The stretch `ab` can be read at position 0 and again at position 3, so it is echoed. No echoed stretch of `s` is longer than two letters.
Example 2
Both `xyzw` and `abcd` are echoed and both are four letters long. The first appearance of `xyzw` starts at position 0 while the first appearance of `abcd` starts at position 8, so `xyzw` wins the tie.
Example 3
`aaa` can be read at position 0 and again at position 1; those two readings overlap, which the rules allow.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def longest_dup_substring(s: str) -> str:public String longestDupSubstring(String s)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.