#K72292. Longest Powerful Substring
Longest Powerful Substring
Longest Powerful Substring
Given a string S of length N, a substring is defined as powerful if it contains at most one distinct character that appears more than once. In other words, if we denote the frequency of a character c in the substring by f(c), then the substring is powerful if
\( \left|\{ c : f(c) > 1 \}\right| \le 1 \)
Your task is to determine the length of the longest powerful substring in S.
Example:
- For S = "abacaba" (N = 7), the longest powerful substring is "abaca" with length 5. Although 'a' appears multiple times, it is the only character that repeats.
- For S = "abcde" (N = 5), all characters are distinct so the whole string is powerful with length 5.
- For S = "aabbcc" (N = 6), one of the longest powerful substrings is "aab" with length 3, but there exists a longer one "abb" starting from index 1 which gives length 3 as well. The maximum achievable length is 4 (for example, "aabb" is not allowed because both 'a' and 'b' repeat, so the best valid one is 4 by considering other segments).
Note: The string indices are 0-based.
inputFormat
The input is given via standard input (stdin) with the following format:
T N1 S1 N2 S2 ... NT ST
Here, T is the number of test cases. For each test case, the first token is an integer N (the length of the string) followed by a string S.
outputFormat
For each test case, output a single integer on a new line, representing the length of the longest powerful substring.
## sample3
7 abacaba
5 abcde
6 aabbcc
5
5
4
</p>