#K72307. Minimal Operations to Achieve Single Unique Character
Minimal Operations to Achieve Single Unique Character
Minimal Operations to Achieve Single Unique Character
Given an integer \( k \) and a string \( s \), you are allowed to perform at most \( k \) operations. In one operation, you may remove all occurrences of a chosen character from \( s \). The goal is to transform \( s \) into a string that contains exactly one unique character.
If it is possible to achieve this within the allowed operations, output the minimal number of operations required (which is \( (\text{number of distinct characters} - 1) \)); otherwise, output \(-1\).
For instance, if \( s = "aaabccc" \) with \( k = 2 \), removing one of the three distinct characters requires 2 operations, and if \( s = "abcdef" \) with \( k = 3 \) it is impossible since 5 removals are needed.
inputFormat
The input is provided via stdin and consists of two lines:
- The first line contains a single integer \( k \) representing the maximum allowed operations.
- The second line contains the string \( s \), composed of lowercase English letters.
outputFormat
Output a single integer via stdout which is the minimal number of operations needed to reduce the string to exactly one unique character if possible; otherwise, output \(-1\).
## sample2
aaabccc
2