All problems

Problem 229

Longest Happy Prefix

Hard · 30 pts · String, String Matching, Rolling Hash

Problem

A happy prefix of a string S is a non-empty prefix that is also a suffix of S, not counting S itself. For example, "abab" has the happy prefix "ab". Print the longest happy prefix of S, or -1 if there is none. This is exactly what the KMP prefix function computes: its value at the last position is the length of the longest proper prefix that is also a suffix. Hashing with binary search or direct comparison also works.

Input

A single string S of lowercase English letters.

Output

Print the longest happy prefix, or -1.

Constraints

1 <= |S| <= 100000

Example input

level

Example output

l

Explanation

In "level", the prefix "l" is also the last letter. "le" does not match the suffix "el", and the longer prefixes do not match either, so the longest happy prefix is "l".

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Remove Duplicate Letters Next: Sum of First N Natural Numbers