#P3590. Longest Valid Substring
Longest Valid Substring
Longest Valid Substring
You are given a string s of length \(n\) that contains only the characters \(B\), \(C\), and \(S\). Find the length of the longest contiguous substring such that the substring either consists of only one unique character, or if it contains multiple distinct characters then no two different characters appear with the same frequency.
Formally, let the substring be \(s[l..r]\) (1-indexed). If the substring contains only one distinct character, it is valid. Otherwise, if the counts \(f_i\) of each character (for every character that appears at least once in \(s[l..r]\)) are such that for any two distinct characters \(i\) and \(j\) we have \(f_i \neq f_j\), then the substring is valid. Your task is to print the length of the longest valid substring.
inputFormat
The first line of input contains a single integer \(n\) \( (1 \le n \le 10^5)\) denoting the length of the string. The second line contains the string \(s\) of length \(n\) consisting only of the characters B
, C
, and S
.
outputFormat
Output a single integer: the length of the longest contiguous substring of \(s\) that meets the conditions specified above.
sample
3
CBS
1