Rearrange String k Distance Apart - Problem

You're given a string s and an integer k. Your task is to rearrange the characters in the string such that no two identical characters are closer than k positions apart.

This is like organizing a seating arrangement where certain people need to be separated by at least k seats! If it's impossible to create such an arrangement, return an empty string "".

Key Challenge: You need to strategically place the most frequent characters first while ensuring they maintain the required distance.

Example: For string "aabbcc" with k=3, one valid arrangement could be "abcabc" - notice how each 'a' is 3 positions apart, same for 'b' and 'c'.

Input & Output

example_1.py — Basic case with k=3
$ Input: s = "aabbcc", k = 3
Output: "abcabc"
💡 Note: Each character appears twice, and with k=3, we need at least 3 positions between same characters. The arrangement 'abcabc' satisfies this: 'a' appears at positions 0 and 3 (distance = 3), 'b' at 1 and 4 (distance = 3), 'c' at 2 and 5 (distance = 3).
example_2.py — Impossible case
$ Input: s = "aaadbbcc", k = 2
Output: ""
💡 Note: Character 'a' appears 3 times, but with k=2 and string length 8, we can only place 'a' at most at positions 0, 2, 4, 6 - that's 4 possible positions for 3 occurrences. However, the arrangement is still impossible due to the distribution of other characters.
example_3.py — Edge case k=0
$ Input: s = "aaab", k = 0
Output: "aaab"
💡 Note: When k=0 or k=1, no rearrangement constraints apply, so we can return the original string or any permutation. The original string itself is valid.

Constraints

  • 1 ≤ s.length ≤ 105
  • 0 ≤ k ≤ s.length
  • s consists of lowercase English letters only
  • If multiple valid arrangements exist, return any one of them

Visualization

Tap to expand
Rearrange String k Distance Apart INPUT String s = "aabbcc" a a b b c c k = 3 (minimum distance apart) Character Frequency a: 2 b: 2 c: 2 Max Heap (by freq) a,b,c All freq=2 ALGORITHM STEPS 1 Count Frequencies Build frequency map 2 Build Max Heap Sort by frequency desc 3 Place Characters Greedy: most freq first 4 Wait Queue Hold chars for k slots Placement Process i=0: Place 'a' ---> [a,_,_,_,_,_] i=1: Place 'b' ---> [a,b,_,_,_,_] i=2: Place 'c' ---> [a,b,c,_,_,_] Repeat ---> [a,b,c,a,b,c] FINAL RESULT Output: "abcabc" a b c a b c Distance Verification 'a' at pos 0, 3 ---> dist = 3 [OK] 'b' at pos 1, 4 ---> dist = 3 [OK] 'c' at pos 2, 5 ---> dist = 3 [OK] Valid Solution! All constraints met Complexity Time: O(n log k) Space: O(n) Key Insight: The greedy approach works by always placing the most frequent character first. Use a max heap to track frequencies and a waiting queue to hold characters that must wait k positions before being placed again. If the heap is empty but queue has waiting chars, rearrangement is impossible. TutorialsPoint - Rearrange String k Distance Apart | Greedy Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
31.7K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen