#C10268. Longest Substring with At Most Two Distinct Characters

    ID: 39454 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 determine the length of the longest contiguous substring that contains at most two distinct characters. For example, if S is "eceba", the longest substring is "ece" which has a length of 3.

The problem can be solved efficiently using a sliding window technique along with two pointers to keep track of the characters and their frequencies.

Note: If the input string is empty, the answer should be 0.

inputFormat

The input consists of a single line containing the string S. The string S is provided via standard input and may contain only alphabetical characters. It could also be an empty string.

outputFormat

Output a single integer representing the length of the longest substring of S that contains at most two distinct characters. The result should be printed to standard output.## sample

eceba
3