#K69882. Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct Characters
Longest Substring with Two Distinct CharactersYou are given a string s. Your task is to find the longest contiguous substring of s that contains exactly two distinct characters. If there are several substrings with the maximum length, output the one which appears first.
Note: A substring is a contiguous sequence of characters within a string. An efficient solution with a time complexity of \( O(n) \) using the sliding-window technique is expected.
Examples:
For s = "abcbbbbcccbdddadacb"
, the output should be "bcbbbbcccb"
.
For s = "aabbcc"
, the output should be "aabb"
.
For s = "abc"
, the output should be "ab"
because only the first two characters form a valid substring with exactly two distinct characters.
inputFormat
The input is read from stdin and consists of a single line containing the string s.
outputFormat
Output the longest substring of s that contains exactly two distinct characters to stdout.
## sampleabcbbbbcccbdddadacb
bcbbbbcccb