#K65847. Rearrangement with K Distinct Characters
Rearrangement with K Distinct Characters
Rearrangement with K Distinct Characters
You are given a string S and an integer K. Your task is to determine whether it is possible to rearrange the characters in S such that:
- No two adjacent characters in the rearranged string are the same.
- The rearranged string contains at least K distinct characters.
If both conditions can be satisfied, output YES; otherwise, output NO.
Note: The input is provided via stdin
and the output must be printed to stdout
. All formulas in the description are given in LaTeX format. For example, the condition for the maximum frequency of any character is given by: \(\text{maxFreq} \le \frac{|S|+1}{2}\).
inputFormat
The input consists of two lines:
- The first line contains the string S.
- The second line contains an integer K.
outputFormat
Output a single line containing either YES or NO (without quotes) based on whether it is possible to rearrange the string satisfying the conditions.
## sampleaabbcc
3
YES