Longest prefix reappearing

Write codeEasy13 min

Your task

A serial console capture from a test rig sometimes hides a reboot nobody noticed, because the board prints the same startup banner every time it comes up. The banner is whatever text the capture opens with, and its length was never recorded, so the tool hunts for the longest opening stretch that turns up again further on. Given capture, return the length of the longest prefix of capture that also appears in full starting at some later position. The second copy is allowed to overlap the first, so a capture of four identical characters answers 3. Matching is exact and case sensitive. Return 0 when no prefix reappears, when the capture holds a single character, and when the capture is empty.

Examples

boot_banner_echo_length(capture: str) -> int
Example 1
Inputboot_banner_echo_length("BOOT v2 ready. temp ok. BOOT v2 ready. temp high.")
Output20
Example 2
Inputboot_banner_echo_length("aaaa")
Output3
Example 3
Inputboot_banner_echo_length("abcdef")
Output0