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):
from string import ascii_lowercase


def main():
    word = input()
    if len(word) < 26:
        count = {}
        for a in ascii_lowercase:
            count[a] = 0
        for w in word:
            count[w] += 1
        count_l = list(count.items())
        count_l.sort(key=lambda x: x[0])
        print(count_l)
        add = ""
        for i in range(26):
            if count_l[i][1] == 0:
                add = count_l[i][0]
                break
        print(word + add)
    elif word == ascii_lowercase[::-1]:
        print(-1)
    else:
        is_printed = False
        while len(word) > 0 and not is_printed:
            for c in ascii_lowercase[ascii_lowercase.index(word[-1]) + 1:]:
                if c not in word[:-1]:
                    print(word[:-1] + c)
                    is_printed = True
                    break
            else:
                word = word[:-1]


if __name__ == '__main__':
    main()



from string import ascii_lowercase


def main():
    word = input()
    if word == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    elif len(word) < 26:
        count = {}
        for a in ascii_lowercase:
            count[a] = 0
        for w in word:
            count[w] += 1
        count = count.items()
        add = ""
        for i in range(26):
            if count[i][1] == 0:
                add = count[i][0]
                break
        print(word + add)
    else:
        stop = ascii_lowercase[::-1]
        final_permutation_index = 25
        for i in range(-2, -27, -1):
            if word[i:] not in stop:
                final_permutation_index = 26 + i
                break
        if final_permutation_index != 25:
            print(word[:final_permutation_index] + word[-1])
        else:
            word = list(word)
            if word[-2] == "z":
                word[-1], word[-3] = word[-3], word[-1]
            else:
                word[-1], word[-2] = word[-2], word[-1]
            print("".join(word))



if __name__ == '__main__':
    main()



from string import ascii_lowercase


def main():
    word = input()
    if len(word) < 26:
        count = {}
        for a in ascii_lowercase:
            count[a] = 0
        for w in word:
            count[w] += 1
        count_l = list(count.items())
        count_l.sort(key=lambda x: x[0])
        add = ""
        for i in range(26):
            if count_l[i][1] == 0:
                add = count_l[i][0]
                break
        print(word + add)
    elif word == ascii_lowercase[::-1]:
        print(-1)
    else:
        is_printed = False
        while len(word) > 0 and not is_printed:
            for c in ascii_lowercase[ascii_lowercase.index(word[-1]) + 1:]:
                if c not in word[:-1]:
                    print(word[:-1] + c)
                    is_printed = True
                    break
            else:
                word = word[:-1]


if __name__ == '__main__':
    main()



Verdicts: Wrong Answer

Runtime Error

Accepted