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):
s = list(input())
m = list('zyxwvutsrqponmlkjihgfedcba')
if len(s) < 26:
    for i in m:
        if not i in s:
            s.append(_)
            break
    print(''.join(s))
else:
    if s == m:
        print(-1)
    else:
        x = s[-1]
        for i in range(25)[::-1]:
            if x > s[i]:
                s = s[:i]
                s.append(i)
                break
            if i == 0:
                z = m.index(s[0])
                s = m[z-1]
        print(''.join(s))



s = list(input())
n = list('abcdefghijklmnopqrstuvwxyz')
if len(s) < 26:
    for _ in m:
        if not _ in s:
            s.append(_)
            break
    print(''.join(s))
else:
    if s == n[::-1]:
        print(-1)
    else:
        x = s[-1]
        for _ in range(25)[::-1]:
            if x > s[_]:
                s = s[:_]
                s.append(x)
                break
        print(''.join(s))

s = input()
if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit(0)
l = list('abcdefghijklmnopqrstuvwxyz')
for i in range(26):
    if l[i] != s[i]:
        print(s[:i-1] + l[i])
        exit(0)

s = list(input())
m = list('zyxwvutsrqponmlkjihgfedcba')
if len(s) < 26:
    for i in m[::-1]:
        if not j in s:
            s.append(j)
            break
    print(''.join(s))
else:
    if s == m:
        print(-1)
    else:
        x = s[-1]
        for i in range(25)[::-1]:
            if x > s[i]:
                s = s[:i]
                s.append(x)
                break
            if i == 0:
                z = m.index(s[0])
                s = m[z-1]
                break
        print(''.join(s))

s = list(input())
m = list('zyxwvuutsrqponmlkjihgfedcba')
if len(s) < 26:
    for _ in m:
        if not _ in s:
            s.append(_)
            break
    print(''.join(s))
else:
    if s == m:
        print(-1)
    else:
        x = s[-1]
        for _ in range(25)[::-1]:
            if x > s[_]:
                s = s[:_]
                s.append(x)
                break
        print(''.join(s))



s = list(input())
m = list('zyxwvutsrqponmlkjihgfedcba')
if len(s) < 26:
    for i in m[::-1]:
        if not i in s:
            s.append(i)
            break
    print(''.join(s))
else:
    x = s[-1]

    for i in range(25)[::-1]:
        if x > s[i]:
            y = m.index(s[i])
            s = s[:i]
            for j in range(y)[::-1]:
                if not m[j] in s:
                    s.append(m[j])
                    break
            break
        else:
            x = s[i]
        if i == 0:
            print(-1)
            exit(0)
    print(''.join(s))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Accepted