Reorganize String Heap Problem

Reorganize String Heap Problem — ExecCode Medium DSA Practice

Solve the Reorganize String Heap problem on ExecCode. Free online medium DSA practice in Heap. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Description Imagine you're solving Reorganize String Heap in a real system where you must be correct and efficient. Problem Given a string s, rearrange the characters so that no two adjacent characters are the same. If multiple valid rearrangements exist, return any one of them. If no such rearrangement is possible, return an empty string. Input Format A single line containing the string s. Output Format A single line containing a valid rearranged string with no identical adjacent characters, or an empty string if no valid rearrangement exists. Constraints s contains only lowercase letters. 1 <= length of s <= 10^5. You may return any valid rearrangement when multiple answers exist.

Examples

Input aab; Output aba

Constraints

1 <= n <= 10^4

Practice Reorganize String Heap free on ExecCode. Browse DSA problems, topic map, and placement guides.