#K16021. Character Arrangement Challenge
Character Arrangement Challenge
Character Arrangement Challenge
You are given a string characters
of lowercase English letters and an integer k
. Your task is to determine whether it is possible to rearrange the characters of the string such that the same characters are at least k positions apart.
The rules are as follows:
- If
k = 1
, then the string is valid only if all characters are unique. - If
k > 1
, a necessary and sufficient condition is that(max_frequency - 1) * k + 1 ≤ n
, wheremax_frequency
is the highest frequency of any character andn
is the length of the string.
Return 1
if such an arrangement is possible, otherwise return 0
.
Examples:
- Input: characters = "aabbcc", k = 3, Output: 1
- Input: characters = "aaabc", k = 1, Output: 0
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a non-empty string of lowercase letters.
- The second line contains an integer
k
representing the minimum required distance between identical characters.
outputFormat
Output a single integer (1
or 0
) to standard output (stdout), where 1
indicates that the characters can be rearranged to satisfy the condition, and 0
indicates that they cannot.
aabbcc
3
1
</p>