#K63337. Longest Unique Substring

    ID: 31731 Type: Default 1000ms 256MiB

Longest Unique Substring

Longest Unique Substring

Given a string S, your task is to find the longest contiguous substring that contains all unique characters. For this substring, output two numbers: the number of unique characters in it and the length of the substring.

Formally, for a string \( S \) of length \( n \), find indices \( i \) and \( j \) (with \( 0 \le i \le j < n \)) such that the substring \( S[i \ldots j] \) has all distinct characters and \( j-i+1 \) is maximized. Let \( L \) be this maximum length and \( U \) be the number of unique characters in that substring (note that \( U = L \) because all characters in the substring are unique).

For example:

  • For S = "abcbcbb", the longest substring with all distinct characters is "abc", so the output is 3 3.
  • For S = "bbbbb", the answer is 1 1.
  • For S = "pwwkew", the answer is 3 3.

You need to read the input from standard input and output your answer to standard output.

inputFormat

The first line of input is an integer \( T \) representing the number of test cases. Each of the following \( T \) lines contains a non-empty string \( S \).

Example:

3
abcbcbb
bbbbb
pwwkew

outputFormat

For each test case, output a line containing two space-separated integers: the number of unique characters in the longest substring of \( S \) with all distinct characters, followed by the length of that substring.

Example:

3 3
1 1
3 3
## sample
3
abcbcbb
bbbbb
pwwkew
3 3

1 1 3 3

</p>