#K3611. Longest Substring with At Most Two Distinct Characters

    ID: 25682 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 substring as S[i...j], then it must satisfy

\(\#\{c \mid c \in S[i...j]\} \le 2\).

If there exist multiple longest substrings, output the lexicographically smallest one.

Example:

  • Input: abcbbbbcccbdddadacb
  • Output: bcbbbbcccb

inputFormat

The input is read from standard input (stdin) and consists of a single line containing a non-empty string S composed of lowercase English letters.

outputFormat

Output to standard output (stdout) the longest substring of S that contains at most two distinct characters. If there are multiple valid answers, output the lexicographically smallest one.

## sample
abcbbbbcccbdddadacb
bcbbbbcccb