#K71037. Longest Substring with Character Frequency Constraint
Longest Substring with Character Frequency Constraint
Longest Substring with Character Frequency Constraint
Given a string s
consisting only of uppercase English letters ('A' to 'Z'), find the length of the longest contiguous substring in which no character appears more than twice.
Formally, for a substring s[l...r]
, it must satisfy that for every character c in the substring, its frequency satisfies \( f(c) \le 2 \).
Example: For s = "AABACDDF"
, the longest valid substring is "AABACDD" with a length of 7.
inputFormat
The first line contains an integer T
representing the number of test cases. Each of the next T
lines contains a string s
consisting solely of uppercase letters.
Note: The input is provided via standard input (stdin).
outputFormat
For each test case, output a single integer on a new line representing the length of the longest substring where every character appears no more than twice.
Output should be written to standard output (stdout).
## sample2
AABACDDF
AABBCC
7
6
</p>