Naruto and Sasuke

5

1 votes
Problem

Naruto and Sasuke, as always leave no ways of showing their abilities and potential to each other. Kakashi, their teacher has finally decided to put an end to this and arranged for a ‘Scroll Test’.
He assigned Scrolls i.e. a Latin character A-Z and kept them at various islands with each islands at unit distance from its previous one.Each island has one scroll and the arrangement is represented as a string. A person is allowed to collect only two scrolls with the same character. Further the collected scrolls should have exactly one island in between them with a scroll of same character otherwise it will release a hypnosis jutsu. Say if the islands are ABACA then they can collect only 1th and 5th scrolls because they are on alternate islands with scrolls of same character. No other scroll pair is allowed. Still jealous of Sasuke’s capabilities, Naruto has decided to pick the scroll with the condition mentioned above and at maximum distance from each other.
Can you tell him what is the maximum distance between islands from where he can collect the scrolls.
If it is not possible print -1;

Constraints:
1 <= |S| <= 100000

Input:
The input consist of string consisiting of charaters from ‘A’-‘Z’ which denotes the scrolls on the islands.

Ouput:
Answer the maximum distance as specified above.


Problem Setter: Swapnil Saxena

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The A's at position 2nd and 7th are allowed as they have a A at position 4 in between them and hence maximum distance between any such allowed pair is 5.

Editor Image

?