Longest Happy Prefix
Real-world version: Longest Overlapping BorderThe statement here is our own: the same inputs, outputs and technique.
A reel of tape carries a label written in lowercase letters, given as tape.
A border of the label is a string that is at the same time a prefix of the label and a suffix of the label, and that is strictly shorter than the whole label. The empty string is a border of every label.
Return the longest border of tape. When the empty string is the only border, return the empty string.
Examples
Example 1
- Input
- tape = "abababab"
- Output
- "ababab"
The label opens with `ababab` and closes with `ababab`, and that string is shorter than the label itself.
Example 2
- Input
- tape = "abcd"
- Output
- ""
The label opens with `a` and closes with `d`, so no string of one or more letters is both a prefix and a suffix. The empty string is returned.
Example 3
- Input
- tape = "aabaa"
- Output
- "aa"
The label opens with `aa` and closes with `aa`, and that string is shorter than the label itself.
Constraints
- 1 <= tape.length <= 10^5
- tape consists of lowercase English letters only.
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_border(tape: str) -> str:public String longestBorder(String tape)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 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
- Shortest PalindromeReal-world version: Bead Strand MirrorHard
- Find the Index of the First Occurrence in a StringReal-world version: Locate the Routing TagEasy
- Repeated Substring PatternReal-world version: Ribbon Repeats a MotifEasy