#K90687. Minimize Distinct Characters After Removals

    ID: 37808 Type: Default 1000ms 256MiB

Minimize Distinct Characters After Removals

Minimize Distinct Characters After Removals

You are given a string s consisting of lowercase English letters and an integer k. You must perform exactly k removal operations. In each operation, you may remove any single character from s. Your goal is to minimize the number of distinct characters in the resulting string.

Observation: Removing all occurrences of a character reduces the count of distinct characters by one. Hence, a greedy strategy is to remove characters which occur the fewest times first.

Formally, if a character appears f times and if k \ge f, you can remove all occurrences of that character, thereby decreasing the number of distinct characters by one. If you cannot completely remove a character (i.e. k < f), then partial removals do not affect the distinct count. Compute the minimum number of distinct characters possible after exactly k removals.

Examples:

  • For k=3 and s="abcde", the answer is 2.
  • For k=2 and s="aaabb", the answer is 1.
  • For k=0 and s="xyz", the answer is 3.

inputFormat

The input is read from standard input (stdin) and consists of two tokens:

  1. An integer k representing the number of removal operations.
  2. A string s containing only lowercase English letters.

The two tokens are separated by whitespace (spaces or newlines).

outputFormat

Output a single integer to standard output (stdout) - the minimum number of distinct characters remaining in the string after exactly k removals.

## sample
3
abcde
2