#K54142. Minimum Modifications to Achieve K Distinct Characters
Minimum Modifications to Achieve K Distinct Characters
Minimum Modifications to Achieve K Distinct Characters
You are given a string s and an integer k. Your task is to determine the minimum number of modifications required so that the number of distinct characters in the string becomes exactly k. A modification is defined as either adding a new character (which is not already present in the string) or removing all occurrences of an existing character.
Let \(d\) be the number of distinct characters in s. The answer is given by \(|d - k|\).
Example:
- For s = "abc" and k = 2, we have \(d=3\), so the answer is \(|3-2| = 1\).
- For s = "a" and k = 2, we have \(d=1\), so the answer is \(|1-2| = 1\).
Note that the string modifications are conceptual: you are not actually required to output the modified string, but only to determine the number of modifications needed.
inputFormat
The input begins with a single integer t (1 ≤ t ≤ 104) representing the number of test cases.
Each test case is provided on a separate line containing a string s (consisting of lowercase English letters, with no spaces) and an integer k (1 ≤ k ≤ 26) separated by a space.
You should read from standard input (stdin).
outputFormat
For each test case, output a single integer on a new line representing the minimum number of modifications required to make the number of distinct characters equal to k. Write your answer to standard output (stdout).
## sample3
abc 2
a 2
xyz 5
1
1
2
</p>