#C14938. Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
Given a string (s) and an integer (k), your task is to compute the length of the longest substring of (s) that contains at most (k) distinct characters. A substring is a contiguous sequence of characters within the string. For example, when (s = "eceba") and (k = 2), the longest substring is "ece" with a length of 3. This problem can be efficiently solved using the sliding window technique with a time complexity of (O(n)), where (n) is the length of the string.
inputFormat
The input is read from standard input (stdin). The first line contains the string (s). The second line contains an integer (k). Note that (s) can be an empty string.
outputFormat
Output to standard output (stdout) a single integer representing the length of the longest substring of (s) that contains at most (k) distinct characters.## sample
eceba
2
3
</p>