#K84652. Longest Substring with K Distinct Characters
Longest Substring with K Distinct Characters
Longest Substring with K Distinct Characters
Given a string S
and an integer K
, find the longest substring of S
that contains exactly K
distinct characters.
This problem can be formally represented as follows: find a substring S'
of S
such that:
$|S'|$ is maximized and $\text{distinct}(S') = K$.
If no such substring exists, output an empty string.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains the string
S
. - The second line contains the integer
K
.
outputFormat
Print to standard output (stdout) the longest substring of S
that contains exactly K
distinct characters. If there is no such substring, print an empty string.
abcba
2
bcb
</p>