PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000154. Sắp xếp lại xâu (Reorganize String)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sắp xếp lại xâu (Reorganize String)

Reorganize String

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Rearrange all the letters in a lowercase string ss so that no two adjacent letters are equal. The number of occurrences of each letter must be preserved.

Print any such rearrangement, or an empty line if no rearrangement exists.

Input

The input contains one nonempty lowercase string ss.

Output

Print a permutation of ss with no equal adjacent letters; if impossible, print an empty line.

Subtasks

  • Subtask 1 (20 points): 1≤∣s∣≤201\le|s|\le20; ss consists only of lowercase a–z.
  • Subtask 2 (30 points): 1≤∣s∣≤1501\le|s|\le150; ss consists only of lowercase a–z.
  • Subtask 3 (50 points): 1≤∣s∣≤5001\le|s|\le500; ss consists only of lowercase a–z.

Examples

Input

aab

Output

aba

Explanation

For aab, a valid answer is aba, whose equal a letters are separated by b. With aaab there are too many a letters for this to be possible, so the correct output is empty.