A lighthouse keeper spent one foggy night firing signal flares in a strict sequence and wrote down the color of every flare as a single lowercase letter, producing one continuous log string. Two flares that share the same color form an echo pair. For an echo pair, the echo gap is the number of flares fired strictly between the two matching flares (the two matching flares themselves are not counted). Your job is to scan the whole log and report the largest echo gap over every possible echo pair of matching colors. If every color in the log was fired at most once, no echo pair exists, and you must report -1 instead.
A single line containing the log string s.
A single integer: the maximum echo gap over all echo pairs, or -1 if no color repeats.
s <= 100000s consists only of lowercase English letters ('a'-'z').Example 1
Input
abca
Expected
2
Explanation
The letter 'a' appears at positions 0 and 3; the two characters strictly between them are 'b' and 'c', giving an echo gap of 2. No other letter repeats, so 2 is the answer.
Example 2
Input
cbzxy
Expected
-1
Explanation
Every letter c, b, z, x, y appears exactly once, so no echo pair exists and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →