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 string

S = input()
alphabetsStr = string.ascii_lowercase
alphabets = list(map(lambda x: x, string.ascii_lowercase))
lastWord = ''.join(alphabets[::-1])

if len(S) == 26:
    if S == lastWord: print(-1)
    else:
        for i  in S:
            if alphabets[i] != S[i]:
                print(S[:i] + alphabets[i])
                break
# else:
#     for s in alphabets:
#         if s not in S:
#             print(S + s)
#             break

print(-1)

import string

S = input()

if len(S) == 26: print(-1)
retS = ''
alphabetsStr = string.ascii_lowercase
alphabets = list(map(lambda x: x, string.ascii_lowercase))
found = False
lenS = len(S)
if S[0] == 'a':
    for i in range(len(S)):
        if S[i] != alphabets[i]:
            retS = string.ascii_lowercase[:i+1]
            found = True
            break
else:
    idx = alphabets.index(S[0])
    for i in range(1, idx+1):
        if S[i] != alphabets[i-1]:
            retS = S[:i] + alphabets[i-1]
            found = True
            break
    if not found:
        for i in range(idx+1, len(S)):
            if S[i] != alphabets[i]:
                retS = string.ascii_lowercase[:i+1]
                found = True
                break
    
if not found: retS = S + alphabets[len(S)]
print(retS)


    

import string

S = input()
alphabets = list(string.ascii_lowercase)
lastWord = ''.join(alphabets[::-1])
cnt = [S.count(chr(ord('a')+i)) for i in range(26)]
if len(S) == 26:
    if S == lastWord: print(-1)
    else:
        for i in S:
            if alphabets[i] != S[i]:
                print(S[:i] + alphabets[i])
                break
else:
    print('test')

import string

S = input()
reversedS = ''.join(list(S)[::-1])
alphabets = list(string.ascii_lowercase)
lastWord = ''.join(alphabets[::-1])
cnt = [S.count(chr(ord('a')+i)) for i in range(26)]
if len(S) == 26:
    if S == lastWord: print(-1)
    else:
        found = False
        for i in range(len(reversedS)):
            s = reversedS[i]
            largeS = alphabets[ord(s)-ord('a')+1:]
            tailS = list(S)[26-i-1:]
            for lS in largeS:
                if lS in tailS:
                    print(S[:26-i-1]+lS)
                    found = True
                    break
            if found: break
else:
    for j in range(26):
        if cnt[j] == 0:
            S += chr(ord('a')+j)
            print(S)
            break


Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted