#C9864. K-Distance Rearrangement
K-Distance Rearrangement
K-Distance Rearrangement
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 any two identical characters are separated by at least k positions. In other words, if the rearranged string is represented as \( a_1 a_2 \cdots a_n \), then for any two indices \( i \) and \( j \) with \( a_i = a_j \) and \( i \neq j \), we must have \( |i - j| \ge k \).
Note: The answer should be YES
if such a rearrangement is possible, and NO
otherwise.
Example:
Input: aaabbcc 2</p>Output: YES
In the above example, one possible rearrangement is "ababcca" (one valid rearrangement exists), and it satisfies the condition with \( k = 2 \). However, if \( k \) is increased to 3
for the same string, no valid rearrangement exists.
inputFormat
The input consists of two lines:
- The first line contains a non-empty string s consisting of lowercase English letters.
- The second line contains an integer k (\(1 \leq k \leq |s|\)).
outputFormat
Output a single line containing YES
if the string can be rearranged to satisfy the given condition, or NO
otherwise.
aaabbcc
2
YES