#C1038. Longest Two Distinct Substring
Longest Two Distinct Substring
Longest Two Distinct Substring
Given a string s
, your task is to find the longest substring that contains at most two distinct characters. If there are multiple answers with the same maximum length, return the one that appears first in the string.
For example:
- For
s = "eceba"
, the answer is"ece"
. - For
s = "ccaabbb"
, the answer is"aabbb"
.
The formal requirement can be expressed as: find the substring s[i...j]
such that the number of distinct characters in it is at most 2 and its length \(L = j-i+1\) is maximized. In case of ties, choose the one with the smallest i
.
inputFormat
The input consists of a single line containing a string s
. The string may be empty. You must read the input from stdin.
Note: The string s
can contain any characters.
outputFormat
Output the longest substring that contains at most two distinct characters. The output should be printed to stdout.
## sampleeceba
ece