Chameleon Substring
Time limit2sMemory limit512 MB
Given a string S, find the longest substring that is both a prefix and a suffix of S and also appears somewhere strictly inside S.
- Level
Medium7 of 10
- Topics
- String matching, String, Prefix sum
- Solved
- No attempts yet
Problem
A substring of a string is a chameleon substring when all three conditions hold.
- is a prefix of .
- is a suffix of .
- also occurs in at a place that is neither of those two. That is, at least one occurrence of neither starts at the first character of nor ends at the last character of .
The three occurrences may overlap.
For example, if fixprefixsuffix, then fix is a chameleon substring. It is a prefix of , it is a suffix of , and it also occurs starting at the 7th character.
Given , find the longest chameleon substring.
Input
The first line contains the string . It consists of lowercase English letters only, and its length is between and .
Output
Print the longest chameleon substring on the first line. A chameleon substring is a prefix of , so its length fixes the string itself and the answer is unique. If no chameleon substring exists, print -1.