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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 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 .
Output
Print a permutation of with no equal adjacent letters; if impossible, print an empty line.
Subtasks
- Subtask 1 (20 points): ; consists only of lowercase
a–z. - Subtask 2 (30 points): ; consists only of lowercase
a–z. - Subtask 3 (50 points): ; 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.