AmazonOA #239
Get Special String
Estimated MediumJudge-ready
Output the smallest string that has the same length as s, is lexicographically strictly greater than s, and has no two equal adjacent characters; if none exists, output −1. The original string itself may already contain adjacent repeats, and z cannot wrap around to a.
Input
One line containing a lowercase string. 1≤length≤1000000.
Output
Output the target string, or −1 if none exists.
Example 1
Input:
abbd
Output:
abca
Example 2
Input:
abccde
Output:
abcdab
Example 3
Input:
zzab
Output:
-1
Example notes
The answers are abca, abcdab and −1. You cannot change only the last letter while keeping repeated characters inside the original string.
Editorial
Loading solution…