6694.String Reorder

Time Limit: 1s Memory Limit: 512MB

Your task is to reorder the characters of a string so that no two adjacent characters are the same. What is the lexicographically minimal such string?

Input Format(From the terminal/stdin)

The only line has a string of length $n$ consisting of characters AZ.

  • $1 \le n \le 10^6$

Output Format(To the terminal/stdout)

Print the lexicographically minimal reordered string where no two adjacent characters are the same. If it is not possible to create such a string, print $-1$ .

Sample Input

Copy
HATTIVATTI
          \n

Sample Output

Copy
AHATITITVT
          \n
Source: CSES, Introductory Problems, 1743

Submit

请先 登录

© 2025 FAQs