#C12288. Unique k Characters
Unique k Characters
Unique k Characters
You are given a string s
and an integer k
. Your task is to construct a new string by taking the first occurrence of each distinct character from s
(in the order they appear) until you have collected k
characters, or you have reached the end of s
.
If the number of unique characters in s
is less than k
, then output all unique characters. Formally, if we denote by \(U\) the ordered set of unique characters extracted from s
, then the answer is the string formed by the first \(\min(|U|, k)\) characters.
For example, for s = "banana"
and k = 3
, the first unique characters are 'b', 'a', 'n', so the answer is "ban"
.
inputFormat
The input consists of two lines:
- The first line contains the string
s
. - The second line contains the integer
k
.
outputFormat
Output a single line containing the resulting string after extracting at most k
unique characters from s
in their original order.
banana
3
ban