#K63587. Longest Substring with Two Distinct Characters

    ID: 31786 Type: Default 1000ms 256MiB

Longest Substring with Two Distinct Characters

Longest Substring with Two Distinct Characters

Given a string s, your task is to find the length of the longest substring that contains at most two distinct characters. In other words, if you consider any substring s[i...j], the number of unique characters in that substring must be less than or equal to 2.

This can be formally described as finding the maximum L such that there exists an index range \( i \leq j \) with: \[ \#\{ s[k] : i \leq k \leq j \} \leq 2 \] where \( \#\{ \cdot \} \) denotes the count of distinct characters.

It is recommended to use a sliding window technique for an efficient O(n) solution.

inputFormat

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

outputFormat

Output a single integer representing the length of the longest substring of s that contains at most two distinct characters.

## sample
aba
3