Under construction

Still being built. Things may change

Rainbow Beads

Time limit1sMemory limit1024 MB

Summary
Find the longest contiguous substring of a string over R, B, V that has no equal adjacent pair under any of the three color-blind views.
Level

Medium6 of 10

Topics
Two pointers, Sliding window, String, Implementation
Solved
No attempts yet

Problem

Jaehyun has a bead made of NN jewels arranged from left to right. Each jewel is one of three colors, Red, Blue, and Violet, written as a single character R, B, or V. As a member of one of the committees of an important contest, Jaehyun wants to use it as a souvenir for a participant.

Jaehyun likes beads with diverse colors, so he defines a bead to be beautiful if every pair of adjacent jewels has different colors. For example, RBVBV is a beautiful bead because every adjacent pair of jewels has different colors. V is a beautiful bead because it has no adjacent pair. However, RBBV is not a beautiful bead, because two Bs are adjacent in the string.

Jaehyun likes not only beads with diverse colors but also a contest with diversity. This time he wants to make a bead that is also colorful to colorblind people. For convenience, we consider only three kinds of people in this problem.

  • Non-colorblind people, who can tell all three colors apart.
  • Red-colorblind people (Protanopia), who cannot tell red and violet apart. They see violet jewels as red jewels.
  • Blue-colorblind people (Tritanopia), who cannot tell red blue and violet apart. They see violet jewels as blue jewels.

Here the string RVB is colorful to non-colorblind people, but it is not colorful to red-colorblind people because a red jewel and a violet jewel are adjacent, and it is also not colorful to blue-colorblind people because a violet jewel and a blue jewel are adjacent.

Jaehyun wants to pick some contiguous part of the bead and cut it out to give as a souvenir. The part he cuts out must be colorful to all three kinds of people. If the whole bead is beautiful, Jaehyun does not necessarily have to cut it out and may simply give the whole bead. What is the length of the longest bead he can give?

Input

The first line contains an integer NN, the length of the bead.

The next line contains a string of length NN in which every character is R, B, or V.

Output

Print the maximum length of a contiguous bead that is colorful to all three kinds of people.

Constraints

  • 1≤N≤250 0001 \le N \le 250\,000

Examples2

  1. Example 1

    Input
    4
    VRRB
    Expected output
    2
  2. Example 2

    Input
    5
    RBBRR
    Expected output
    2