Example
Input
s = 'araaci', k = 2
Output
4
The longest substring with at most 2 distinct characters is 'araa' or 'araa' length 4.
Full lesson preview
Given a string, find the length of the longest substring that contains at most K distinct characters using a sliding-window and frequency map.
Problem statement
Task
Examples
Input
s = 'araaci', k = 2
Output
4
The longest substring with at most 2 distinct characters is 'araa' or 'araa' length 4.
Input format
Output format
Constraints
Samples
Input
s = 'cbbebi', k = 3
Output
5
The longest substring with at most 3 distinct characters is 'cbbeb' or 'bbebi' with length 5.