Problem: Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible **diverse** words in lexicographical order.  A word is called **diverse** if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, `atcoder`, `zscoder` and `agc` are diverse words while `gotou` and `connect` aren't diverse words.  Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
import sys
from collections import deque
from collections import Counter
s = deque(map(str, input().rstrip()))

if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    sys.exit()

s = [ord(x) for x in s]

if len(s) == 26:
    maxlen = True
    s = s[::-1]
    x = s[0]
    for i, w in enumerate(s):
        if x > w:
            break

    s = s[::-1]
    s = s[:-i - 1]

can_use = set(range(97, 123)) - set(s)
if maxlen:
    s = s + [min(can_use) + 1]
else:
    s = s + [min(can_use)]

s = [chr(x) for x in s]

print("".join(s))

import sys
from collections import deque
from collections import Counter
s = deque(map(str, input().rstrip()))

if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    sys.exit()

s = [ord(x) for x in s]

if len(s) == 26:
    maxlen = True
    s = s[::-1]
    x = s[0]
    for i, w in enumerate(s):
        if x > w:
            break

    s = s[::-1]
    s = s[:-i - 1]

can_use = set(range(97, 123)) - set(s)
if maxlen:
    can_use = can_use - {min(can_use)}
s = s + [min(can_use)]

s = [chr(x) for x in s]

print("".join(s))

import sys
s = deque(map(str, input().rstrip()))

if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    sys.exit()

s = [ord(x) for x in s]

if len(s) < 26:
    can_use = set(range(97, 123)) - set(s)
    s = s + [min(can_use)]

else:
    add = []
    cnt = 0
    while s:
        cnt += 1
        q = s.pop()
        add.append(q)
        if q == 122:
            break
    add = [x for x in add if x > s[-1]]
    
    if cnt != 1:
        s[-1] = min(add)

s = [chr(x) for x in s]

print("".join(s))

import sys
from collections import deque
s = deque(map(str, input().rstrip()))


if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    sys.exit()


s = [ord(x) for x in s]


if len(s) < 26:
    can_use = set(range(97, 123)) - set(s)
    s = s + [min(can_use)]


else:
    add = []
    s = s[::-1]
    prev = s[0]
    add.append(prev)

    
    if prev == 122:
        s[1] = 122
        s = s[::-1][:-1]

    
    
    else:
        for i in range(1, len(s)):
            if prev > s[i]:
                add = [x for x in add if x > s[i]]
                s[i] = (min(add))
                s = s[::-1][:-i]
                break
            prev = s[i]
            add.append(prev)


s = [chr(x) for x in s]

print("".join(s))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted