Suki's Scent Trail
Suki, a curly-coated dog with a grey beard and white paws, loves following scent trails through the park. Today, she's tracking a winding path marked by another dog's scent—a long string of smells. She wants to identify the oldest part of the trail she can re-recognize by matching the end of the trail against earlier segments. The older the part she can match, the longer she's been following this trail.
You are given a scent trail represented as a string. Find the length of the longest suffix of this string that also appears entirely within the part of the string before it.
Input
The first line contains n (1 ≤ n ≤ 2·10^5), the length of the scent trail.
The second line contains a string s of length n consisting of lowercase English letters.
Output
Print the length of the longest suffix of s that appears at least once as a substring in s[0:n-k] where k is the length of that suffix. If no suffix appears earlier in the string, print 0.
Notes
A suffix of length k is the substring s[n-k:n]. For this suffix to count, it must appear at least once in the first n-k characters of the string.
In the first example, the string is "abcab". The suffix "ab" (length 2) appears at position 0 in the prefix "abc", so the answer is 2. The longer suffix "cab" does not appear earlier, and neither do longer suffixes.