#P1114. Maximum Contiguous Even Gender Pairing
Maximum Contiguous Even Gender Pairing
Maximum Contiguous Even Gender Pairing
In this problem, you are given a sequence of participants arranged in increasing order of their heights. Each participant is represented by a character: 'M' for male and 'F' for female.
The task is to choose a contiguous subarray from the sequence such that the number of males is equal to the number of females. Since the event organizer wants to have as many participants as possible, you need to determine the maximum number of people that can be selected satisfying this condition.
Mathematically, if you denote the number of males as M and the number of females as F in the chosen subarray, then the condition is:
\( M = F \)
Find the maximum length of such a subarray. If there is no valid subarray, output 0.
inputFormat
The input consists of two lines:
- The first line contains a single integer n (1 ≤ n ≤ 105), representing the total number of participants.
- The second line contains a string of length n composed solely of the characters 'M' and 'F', representing the genders of the participants in order.
outputFormat
Output a single integer representing the maximum number of participants in a contiguous subarray where the number of males equals the number of females.
sample
6
MFMMFF
6