Trains the technique from
LeetCode 1392Longest Happy PrefixThis 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 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.
Example 1
The label opens with `ababab` and closes with `ababab`, and that string is shorter than the label itself.
Example 2
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
The label opens with `aa` and closes with `aa`, and that string is shorter than the label itself.
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_border(tape: str) -> str:public String longestBorder(String tape)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.