#C14448. Longest Substring with At Most Two Distinct Characters
Longest Substring with At Most Two Distinct Characters
Longest Substring with At Most Two Distinct Characters
You are given a string s
. Your task is to determine the length of the longest substring of s
that contains at most two distinct characters.
A substring is a contiguous sequence of characters within the string. For example, if s = "eceba"
, the longest substring with at most two distinct characters is "ece", which has a length of 3.
You can use the sliding window technique to solve this problem. In particular, the size of the window at any point can be described by the formula: \( window\_size = right - left + 1 \), where left
and right
are the indices marking the beginning and end of the window respectively.
Examples:
- Input:
eceba
→ Output:3
- Input:
ccaabbb
→ Output:5
inputFormat
The input consists of a single line containing the string s
. The string may be empty and will not contain spaces.
outputFormat
Output a single integer, representing the length of the longest substring of s
that contains at most two distinct characters.
eceba
3