Reorganize String
Medium Heap / Priority Queue AI round
s is a string of lower case letters.
Rearrange the letters of s so that no two equal letters stand side by side. Every letter of s must be used exactly once: none may be left out, added or changed.
Return any such arrangement; every one of them is accepted. If no arrangement keeps equal letters apart, return the empty string "".
Example 1
Input: s = "noon"
One right answer: "nono"
nono keeps the two n's apart and the two o's apart. onon is right too, and every right arrangement is accepted.
Example 2
Input: s = "zzzy"
One right answer: ""
Example 3
Input: s = "mississippi"
One right answer: "ipisisismsp"
Constraints
1 ≤ s.length ≤ 2 × 10⁵suses only the lower case lettersatoz
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Heap / Priority Queue
- Top K Frequent ElementsAI round · Medium
- Kth Largest Element in an ArrayAI round · Medium
- Task SchedulerMedium
- Sort Characters By FrequencyMedium
- K Closest Points to OriginMedium