#K9306. Longest Substring with Exactly Two Colors

    ID: 38335 Type: Default 1000ms 256MiB

Longest Substring with Exactly Two Colors

Longest Substring with Exactly Two Colors

You are given a sequence of beads represented by a string of uppercase English letters and an integer n which is the length of the string. Each bead represents a color. Your task is to find the length of the longest contiguous substring that contains beads of exactly two distinct colors.

More formally, if the beads are represented as \(b_1, b_2, \dots, b_n\), you need to determine the maximum integer \(L\) such that there exists a contiguous segment \(b_i, b_{i+1}, \dots, b_j\) (with \(L=j-i+1\)) in which the number of unique beads is exactly 2.

Note: The input and output should be handled via standard input (stdin) and standard output (stdout) respectively.

inputFormat

The input consists of two lines:

  1. The first line contains a single integer n \( (1 \le n \le 10^5) \), which represents the number of beads.
  2. The second line contains a string of length n consisting of uppercase English letters, representing the colors of the beads.

outputFormat

Output a single integer that denotes the length of the longest contiguous substring that contains exactly two distinct colors.

## sample
10
AABBBCBBBA
7

</p>