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
๐ŸŽต VIP Concert Seating with Social DistancingSeating Chart (k=3 distance required)APos 0BPos 1CPos 2APos 3BPos 4CPos 5VIP Priority ListGroup A (3 people)Group B (2 people)Group C (2 people)Cooldown LoungeRecently seated VIPswaiting for cooldown to expire(must wait k=3 positions)๐ŸŽฏ Seating Strategy1. Priority Queue: Always seat largest remaining VIP group2. Cooldown System: Track when each group can be seated again3. Availability Check: Move groups from cooldown back to priority list4. Impossibility Detection: If no group available, arrangement failsโœ… Success MetricsAll VIP groups seated with proper k=3 social distancingGroup A: positions 0โ†”3 (distance=3) โœ“Groups B,C: positions 1โ†”4, 2โ†”5 (distance=3) โœ“k=3
Understanding the Visualization
1
Count VIP Guests
Count how many of each VIP type we need to seat (character frequencies)
2
Priority Seating
Always seat the most numerous VIP group first (max heap by frequency)
3
Cooldown Period
After seating a VIP, they enter a cooldown - can't be seated again for k positions
4
Cycle Management
As cooldown expires, VIPs become available for seating again
Key Takeaway
๐ŸŽฏ Key Insight: By prioritizing the most frequent characters (largest VIP groups) and using a systematic cooldown mechanism, we maximize our chances of successful arrangement while maintaining required distances.
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