A regional dispatch center keeps a running transmission log as a single line of uppercase callsign letters, one letter recorded per time slot, in the order the callsigns keyed up. Auditors define a monitoring window as any contiguous stretch of the log. A window is considered interference-safe only if no single callsign letter keys up more than twice within it. Find the length of the longest interference-safe window in the log.
s, a string of uppercase English letters.Print a single integer: the length of the longest contiguous window of s in which every letter appears at most twice.
s consists only of uppercase English letters 'A'-'Z'.Example 1
Input
AABCA
Expected
4
Explanation
The full log 'AABCA' is invalid because 'A' keys up three times. The window 'ABCA' (dropping the leading 'A') has 'A' twice, 'B' once, and 'C' once -- every letter at most twice -- and has length 4, the longest such window.
Example 2
Input
XYYZXX
Expected
5
Explanation
The full log has 'X' three times, so it is invalid. The window 'XYYZX' (the first five letters) has 'X' twice, 'Y' twice, and 'Z' once, so it is interference-safe with length 5, the maximum possible.
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 →