#K42877. First N Unique Characters

    ID: 27185 Type: Default 1000ms 256MiB

First N Unique Characters

First N Unique Characters

Given a string (s) and an integer (n), your task is to output the first (n) unique characters from (s) in the order that they first appear. A character is considered unique if it has not been seen before in the string. If (n \leq 0), output an empty string. If (n) is greater than the number of unique characters in (s), output all unique characters.

Examples:

  • Input: s = "teststring", n = 4 → Output: "tesr"
  • Input: s = "abcdefghijk", n = 5 → Output: "abcde"
  • Input: s = "abcabcabc", n = 3 → Output: "abc"
  • Input: s = "aaaaaaa", n = 1 → Output: "a"
  • Input: s = "teststring", n = 0 → Output: "" (empty string)

The problem can be formally stated as follows:

  • Let (U(s)) denote the sequence of unique characters in (s) in the order of their first appearance. Then the output should be the prefix of (U(s)) of length (min(n, |U(s)|)).

inputFormat

The input consists of two lines. The first line contains the string (s). The second line contains the integer (n).

outputFormat

Output the first (n) unique characters from the string (s) in the order they appear. If (n \leq 0) or no characters are available, output an empty string.## sample

teststring
4
tesr