#K85457. Longest Substring with At Most Two Distinct Characters

    ID: 36645 Type: Default 1000ms 256MiB

Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most Two Distinct Characters

You are given a string s. Your task is to find the longest contiguous substring of s that contains at most two distinct characters.

For example, if s = "eceba", then one valid answer is "ece" (another valid answer could be "cec", but your program only needs to return one correct answer according to the algorithm described below). Similarly, if s = "ccaabbb", the longest substring is "aabbb".

The challenge requires you to implement an efficient solution often using the sliding window technique. Mathematically, if you let L(i, j) denote a substring from index i to j of s, you need to find indices i and j such that:

$$\text{max}_{i,j} \{j - i + 1 \mid L(i,j) \text{ has at most 2 distinct characters}\}$$

If there are multiple answers with the same maximum length, you may return any one of them.

inputFormat

The input consists of a single line containing the string s. The string may contain alphabets or other characters. If the input is empty, print an empty string.

Input is provided via standard input (stdin).

outputFormat

Output the longest contiguous substring of s that contains at most two distinct characters. Output the answer to standard output (stdout).

## sample
eceba
ece