Along a foggy coastline, n lanterns are mounted in a fixed row, numbered 0 to n-1, and each lantern repeatedly flashes a signal code, a string of lowercase letters. Define the span between two lanterns at positions i and j (with i < j) as j - i + 1, the number of lanterns from the earlier one to the later one, inclusive. A pair of lanterns is a mismatch if their signal codes are different. Among all mismatched pairs, find the maximum possible span. If every lantern flashes the exact same code (no mismatched pair exists), the answer is 0.
n — the number of lanterns.n lines: each contains one signal code, a non-empty string of lowercase English letters, for lanterns 0 through n-1 in order.A single integer: the maximum span between two mismatched lanterns, or 0 if no mismatch exists.
Example 1
Input
3 fog fog clear
Expected
3
Explanation
Lantern 0 and lantern 1 both flash `fog`, so that pair is not a mismatch. Lantern 0 flashes `fog` while lantern 2 flashes `clear` — a mismatch with span 2-0+1=3, which is the best available among all pairs.
Example 2
Input
5 red amber green red red
Expected
4
Explanation
Lantern 1 flashes `amber` and lantern 4 flashes `red`; since these codes differ, this pair is a mismatch with span 4-1+1=4. No other mismatched pair (for example lantern 0 `red` vs lantern 2 `green`, span 3) reaches a larger span, so 4 is the answer.
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 →