cswork / OAOpen practice workspace

AmazonOA #156

Find Lexicographically Smallest String

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyStrings

Given a lowercase string, find the smallest string of the same length that is lexicographically strictly greater and has no two equal adjacent characters. The original string may already contain adjacent repeats. If no such string exists, output -1.

Input

One line with a non-empty lowercase string. This site sets the length to 1..100000.

Output

Output the target string; if there is no solution, output -1.

Example 1

Input:

abbcd

Output:

abcab

Example 2

Input:

az

Output:

ba

Example 3

Input:

zz

Output:

-1

Example notes

Example 1: keep the prefix ab, raise the third character b to c, and fill with ab, giving abcab. Example 2: the second character z cannot be raised, so raise the first character a to b and fill with a, giving ba. Example 3: zz is already the largest string of length 2, so there is no solution: -1.

Editorial

Loading solution…