#K90687. Minimize Distinct Characters After Removals
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
ands="abcde"
, the answer is2
. - For
k=2
ands="aaabb"
, the answer is1
. - For
k=0
ands="xyz"
, the answer is3
.
inputFormat
The input is read from standard input (stdin) and consists of two tokens:
- An integer
k
representing the number of removal operations. - 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.
3
abcde
2