PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000159. Đoạn dài nhất cân bằng ba ký tự (Longest Balanced Three-Character Substring)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đoạn dài nhất cân bằng ba ký tự (Longest Balanced Three-Character Substring)

Longest Balanced Three-Character Substring

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a string ss of length nn over A, B, C, a nonempty contiguous substring is balanced if the counts of all three characters are equal. Find the maximum length of a balanced substring, or print 0 if none exists.

Input

The first line contains integer nn. The second line contains the string ss of length nn.

Output

Print one integer: the maximum qualifying length, or 0.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; ∣s∣=n|s|=n; all characters belong to A, B, C.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; ∣s∣=n|s|=n; all characters belong to A, B, C; exactly one character is C.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; ∣s∣=n|s|=n; all characters belong to A, B, C.

Example 1

Input

6
AABCBC

Output

6

Explanation

The entire string contains two A, two B and two C characters, so length six is feasible and cannot be exceeded.

Example 2

Input

3
AAB

Output

0

Explanation

There is no C. Every nonempty interval contains A or B, so the three counts cannot be equal.