cswork / OAOpen practice workspace

AmazonOA #252

Longest Perfect Anagrams

Estimated HardJudge-ready

Topics (AI-labelled)StringsHashing

Two strings in which every character occurs the same number of times, but whose contents are not exactly the same, form a perfect anagram pair. Find the maximum length of two contiguous substrings of the original string that form such a pair; they may overlap, two positions with identical content do not count, and output −1 if no such pair exists.

Input

One line containing the string. 1≤length≤100000, lowercase letters.

Output

Output a single integer, the answer.

Example 1

Input:

abcacb

Output:

4

Example 2

Input:

cabcab

Output:

3

Example 3

Input:

aabbcc

Output:

-1

Example notes

The answers are 4, 3 and −1. In the first example bcac and cacb have length 4 and overlap, which is valid.

Editorial

Loading solution…