LC: 358. Rearrange String k Distance Apart - spiralgo/algorithms GitHub Wiki

358. Rearrange String k Distance Apart:

The Essence:

  • The letter we choose shouldn't have been encountered at least k digits ago.

    Therefore, we either need to keep the track of the next valid index for each character and store the information in an array or divide the string into layers by k length from the very beginning.

  • It must have the highest count.

    It means, we also keep track of the element that has the maximum count at that moment.

Details:

For a detailed explanation and the implementations:

https://github.com/spiralgo/algorithms/pull/280