#C7964. Longest Good Substring

    ID: 51893 Type: Default 1000ms 256MiB

Longest Good Substring

Longest Good Substring

You are given a string ( S ) of length ( n ). A substring of ( S ) is called "good" if it contains at most two distinct characters. Your task is to determine the length of the longest good substring.

For a substring ( S[i..j] ) (where ( 0 \le i \le j < n )), if it contains at most two distinct characters, then it is considered good. The answer is defined as ( j - i + 1 ) for the maximum valid substring.

It is recommended to use an efficient sliding window technique to solve this problem.

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains an integer ( n ), which is the length of the string.
  • The second line contains the string ( S ) consisting of lowercase English letters.

outputFormat

Output a single integer to standard output (stdout), which is the length of the longest good substring (i.e. a substring containing at most two distinct characters).## sample

10
abcbaacabd
4

</p>