#K1541. Longest Substring with At Most Two Distinct Characters

    ID: 24350 Type: Default 1000ms 256MiB

Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most Two Distinct Characters

Given a string S, find the longest contiguous substring of S that contains at most two distinct characters. In other words, if we denote the number of distinct characters in a substring T by \( f(T) \), your task is to find the substring T such that \( f(T) \leq 2 \) and the length of T is maximized.

For example, given S = "abcabcabc", a valid answer is ab with length 2. Similarly, if S = "aaabbbccc", then one of the longest substrings that satisfy the condition is aaabbb with length 6.

Note: The condition can be formalized as follows: Given a substring \( T \) of S, it is valid if and only if \( |\{ c : c \in T \}| \leq 2 \). The output should report both the length of this substring and the substring itself.

inputFormat

The input consists of a single line containing the string S.

Constraints:

  • S may contain any characters.
  • The length of S is at most 100000.

outputFormat

Output a single line containing two values separated by a space: the length of the longest valid substring and the substring itself. If the input string is empty, output 0 (zero followed by a space).

## sample
abcabcabc
2 ab