#K55767. Longest Balanced Subsequence
Longest Balanced Subsequence
Longest Balanced Subsequence
Given a sequence consisting of only the characters 'A' and 'B', your task is to find the length of the longest contiguous subsequence where the number of 'A's is equal to the number of 'B's.
Detailed Description:
You are provided with an integer N and a string S of length N. Find the maximum length L such that there exists a contiguous substring of S where the number of 'A's is equal to the number of 'B's. Formally, if we denote the substring from index i to j by S[i..j], then we require:
$$\#A(S[i..j]) = \#B(S[i..j])$$
and L = j - i + 1 is maximized. If there is no such substring, output 0.
inputFormat
The input is provided via standard input (stdin) and consists of two lines:
- The first line contains an integer N representing the length of the sequence.
- The second line contains a string of length N formed only by the characters 'A' and 'B'.
outputFormat
Output a single integer to standard output (stdout) indicating the length of the longest contiguous subsequence with an equal number of 'A's and 'B's. If no such subsequence exists, output 0.
## sample6
AABABB
6