#C12987. Longest Substring with Two Distinct Characters

    ID: 42474 Type: Default 1000ms 256MiB

Longest Substring with Two Distinct Characters

Longest Substring with Two Distinct Characters

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

A substring is a contiguous sequence of characters within the string. Formally, if we denote the set of distinct characters in a substring as \(D\), then the substring is valid if: \[ |D| \le 2 \]

If there are multiple substrings that satisfy the conditions with the same maximum length, return the one that appears first.

Example: Given s = "eceba", the answer is "ece" because it is the longest substring containing at most two distinct characters.

inputFormat

The input is read from stdin and consists of a single line containing the string s.

Constraints:

  • s may be empty.
  • s consists of lowercase English letters only.

outputFormat

Output the longest substring (printed to stdout) that contains at most two distinct characters.

## sample