#C12744. Longest Substring with At Most Two Distinct Characters
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 \(2\) distinct characters. This problem can be solved optimally using a sliding window (two pointers) approach.
Examples:
- For s = "eceba", the longest substring is "ece".
- For s = "ccaabbb", the longest substring is "aabbb".
Note: If there are multiple substrings with the maximum length, output the one which appears first.
inputFormat
The input consists of a single line containing the string s.
outputFormat
Print the longest contiguous substring of s that contains at most 2 distinct characters.
## sampleeceba
ece