#C10322. Longest Substring with At Most Two Distinct Characters

    ID: 39515 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 substring of s that contains at most two distinct characters. The answer should be computed using an efficient algorithm (for example, the sliding window technique) to handle large inputs.

The problem can be formally stated as:

Given a string s of length n, find the substring s[i...j] (with 0 ≤ i ≤ j < n) such that there are at most two distinct characters in the substring, and its length is maximized. If there are multiple answers, output any of them.

Note: The input will be provided via standard input (stdin) and the result should be printed to standard output (stdout).

inputFormat

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

Constraints:

  • 1 ≤ |s| ≤ 105
  • s consists of English letters.

outputFormat

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

## sample
eceba
ece