#K40812. Rearrange String k Distance Apart
Rearrange String k Distance Apart
Rearrange String k Distance Apart
You are given a string s
consisting of lowercase English letters and an integer k
. Your task is to determine whether it is possible to rearrange the letters of s
such that any two identical characters are at least k positions apart.
More formally, consider an arrangement of the characters in s
into a new string. For every pair of equal characters, if their positions in the new string are i
and j
(with i < j
), then it must hold that j - i ≥ k
(with positions being 1-indexed). If such an arrangement exists, output YES
, otherwise output NO
.
Note: When k <= 1
, any arrangement trivially satisfies the condition.
inputFormat
The input consists of two items provided on stdin:
- A string
s
on the first line. - An integer
k
on the second line, representing the minimum required distance between the same characters.
outputFormat
Output a single line with YES
if it is possible to rearrange the string so that the same characters are at least k positions apart. Otherwise, output NO
.
aabbcc
2
YES
</p>