#K40327. Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
Longest Substring with At Most K Distinct Characters
You are given a string ( s ) and an integer ( k ). The task is to find the longest contiguous substring of ( s ) that contains at most ( k ) distinct characters. In other words, if you consider any substring of ( s ), it is valid if the number of unique characters in it is ( \leq k ), and among all such substrings, you must output the one with the maximum length. For example, if ( s = \texttt{eceba} ) and ( k = 2 ), the longest valid substring is ( \texttt{ece} ).
inputFormat
The input is provided via standard input (stdin) and consists of two lines. The first line contains the string ( s ), and the second line contains the integer ( k ).
outputFormat
Output the longest contiguous substring of ( s ) which contains at most ( k ) distinct characters to standard output (stdout). If no valid substring exists, output an empty string.## sample
eceba
2
ece