#K79092. Longest Substring with At Most Two Distinct Characters

    ID: 35231 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.

You can solve this problem using a sliding window approach. In particular, maintain a window [i,j] that satisfies the condition and update it as you traverse the string. The condition can be mathematically represented as: \( |\{ s[k] : i \leq k \leq j \}| \leq 2 \).

If there are multiple valid answers, return the first one encountered with the maximum length.

inputFormat

The input consists of a single string s provided via standard input (stdin). The string can be empty or contain any printable characters.

outputFormat

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

## sample
eceba
ece