#K39377. Longest Substring with At Most Two Distinct Characters

    ID: 26407 Type: Default 1000ms 256MiB

Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most Two Distinct Characters

Given a string s, your task is to find the longest contiguous substring that contains at most two distinct characters. If there are multiple valid answers, output any one of them. This problem tests your ability to maintain a sliding window and efficiently update the window when the constraint is violated.

Formally, for a given string \(s\), find a substring \(s[i\ldots j]\) such that the number of distinct characters in it is at most 2, and \(j - i + 1\) is maximized. If \(|s| < 3\), then the entire string is the answer.

inputFormat

The input is read from standard input. It contains a single line with the string s. The string consists of lowercase English letters.

outputFormat

Output to standard output the longest contiguous substring of s that contains at most two distinct characters. If there are multiple answers, you may print any one of them.

## sample
eceba
ece