#K91212. Longest Substring with Exactly K Distinct Characters
Longest Substring with Exactly K Distinct Characters
Longest Substring with Exactly K Distinct Characters
Given a string S and an integer K, your task is to find the length of the longest substring that contains exactly K distinct characters.
Note: If there is no such substring, print 0.
For example, in the string "abaccc" with K = 2, the longest substring with exactly 2 distinct characters is "abac" which has a length of 4.
The problem requires an efficient solution that typically makes use of the sliding window technique.
Input is provided via standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The input consists of two tokens separated by whitespace. The first token is the string S and the second token is the integer K.
For example:
abaccc 2
outputFormat
Output a single integer, the length of the longest substring of S that contains exactly K distinct characters. If no such substring exists, output 0.
## sampleabaccc 2
4