Longest Duplicate Substring
Real-world version: Longest Repeated Stretch on a Log LineThe statement here is our own: the same inputs, outputs and technique.
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 "".
Examples
Example 1
- Input
- s = "abcabd"
- Output
- "ab"
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
- Input
- s = "xyzwxyzwabcdabcd"
- Output
- "xyzw"
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
- Input
- s = "aaaa"
- Output
- "aaa"
`aaa` can be read at position 0 and again at position 1; those two readings overlap, which the rules allow.
Constraints
- 2 <= s.length <= 3 * 10^4
- s consists of lowercase English letters.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_dup_substring(s: str) -> str:public String longestDupSubstring(String s)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Find Beautiful Indices in the Given Array IReal-world version: Nearby Marker PositionsMedium
- Repeated DNA SequencesReal-world version: Ten-Letter Runs That Come Round AgainMedium
- Maximum Length of Repeated SubarrayReal-world version: Longest Shared Run of Road SegmentsMedium
- Shortest PalindromeReal-world version: Bead Strand MirrorHard