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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đ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 of length 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 . The second line contains the string of length .
Output
Print one integer: the maximum qualifying length, or 0.
Subtasks
- Subtask 1 (20 points): ; ; all characters belong to
A,B,C. - Subtask 2 (30 points): ; ; all characters belong to
A,B,C; exactly one character isC. - Subtask 3 (50 points): ; ; 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.