#K54557. Longest Substring with At Most K Distinct Characters

    ID: 29779 Type: Default 1000ms 256MiB

Longest Substring with At Most K Distinct Characters

Longest Substring with At Most K Distinct Characters

Given a string \( S \) and an integer \( K \), find the longest substring of \( S \) that contains at most \( K \) distinct characters. If there are multiple solutions, return the one that appears first in \( S \).

For example, when \( S = "eceba" \) and \( K = 2 \), the answer is "ece".

inputFormat

The input is given via standard input (stdin) with two lines. The first line is the string \( S \) and the second line is an integer \( K \).

outputFormat

Output the longest substring of \( S \) that contains at most \( K \) distinct characters. The answer should be printed to standard output (stdout).

## sample
eceba
2
ece