Example 1
Input
s = "araaci", k = 2
Output
4
Longest substring with exactly 2 distinct characters is "araa" (length 4).
Full lesson preview
Given a string and an integer K, find the length of the longest substring that contains exactly K distinct characters.
Problem statement
Task
Examples
Input
s = "araaci", k = 2
Output
4
Longest substring with exactly 2 distinct characters is "araa" (length 4).
Input format
Output format
Constraints
Samples
Input
s = "cbbebi", k = 3
Output
5
Longest substrings with exactly 3 distinct characters include "cbbeb" and "bbebi" (length 5).