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⁵
  • s uses only the lower case letters a to z

Watch the explanation

More Heap / Priority Queue

All Heap / Priority Queue problems