#C12482. Longest Two Distinct Characters Substring

    ID: 41914 Type: Default 1000ms 256MiB

Longest Two Distinct Characters Substring

Longest Two Distinct Characters Substring

Given a string s, find the length of the longest contiguous substring that contains exactly two distinct characters.

Formally, let \( s \) be a string of length \( n \) (i.e., \( n = |s| \)). You are required to determine the maximum length of any substring of \( s \) that contains exactly 2 distinct characters.

For example:

  • For s = "eceba", the longest valid substring is "ece", so the answer is 3.
  • For s = "ccaabbb", one valid answer is "aabbb", so the answer is 5.
  • For s = "abaccc", one valid answer is "abac", so the answer is 4.

If no such substring exists, output 0.

inputFormat

The input consists of a single line containing the string s.

You should read the input from standard input (stdin).

outputFormat

Output a single integer: the length of the longest contiguous substring of s that contains exactly two distinct characters. Print the result to standard output (stdout).

## sample
a
0