Last updated
Was this helpful?
Last updated
Was this helpful?
Given a non-empty stringsand an integerk, rearrange the string such that the same characters are at least distancekfrom each other.
All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string""
.
Example 1:
Example 2:
Example 3:
这题,不看答案不会做。自己只想到数频率这一步,数完以后,用另外一条array来存下一个可以放的位置。一开始所有字母都能从0开始。然后一边填一边更新这个位置信息(+k)。填的时候每次多把出现频率较大的那个拿出来填,填完以后更新下一个能填的位置和把频率减一表示已经填好一个了。然后下一个循环找下一个填的字母。