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*;from itertools import*
b=ascii_lowercase;r=s=input();d=l=len(s);t=s[::-1]
if l>25:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(s[::-1],s[-2::-1])])][0]-2;s,b,o=s[:d],s[d+1:],1
print(-(d<-26)or s+sorted(set(b)-set([s,b[:ord(s[d])-97]][o]))[0])

from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    x = z = s, y = b, d = 0
else:
    d = [p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], s[-2::-1])])][0] - 2
    x, y, z = s[:d], s[d+1:], b[:ord(s[d]) - 97]
print(-(d < -26) or x + sorted(set(y) - set(z))[0])

from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    x = z = s, y = b, d = 0
else:
    d = [p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], t[-2::-1])])][0] - 2
    x, y, z = s[:d], s[d+1:], b[:ord(s[d]) - 97]
print(-(d < -26) or x + sorted(set(y) - set(z))[0])

from string import*
from itertools import*
b = ascii_lowercase
s = input()
l = len(s)
if l < 26:
    print(s + sorted(set(b) - set(s))[0])
else:
    d = [p,*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], t[-2::-1])])][0] - 2
    print(-(d < -26) or s[:d] + sorted(set(s[d+1:]) - set(b[:ord(s[d]) - 97]))[0])

from string import*;from itertools import*;b=ascii_lowercase;r=s=input();d=l=len(s);t=s[::-1]
if l>25:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(s[::-1],s[-2::-1])])][0]-2;s,b,o=s[:d],s[d+1:],1
print(-(d<-26)or s+sorted(set(b)-set([s,b[:ord(s[d])-97]][o]))[0])

from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    x = z = s, y = b, d = 0
else:
    d = -[p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], s[-2::-1])])][0] - 2
    x, y, z = s[:d], s[d+1:], b[:ord(s[d]) - 97]
print(-(d < -26) or x + sorted(set(y) - set(z))[0])

from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    print(s + sorted(set(b) - set(s))[0])
else:
    d = [p,len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], t[-2::-1])])][0] - 2
    print(-(d < -26) or s[:d] + sorted(set(s[d+1:]) - set(b[:ord(s[d]) - 97]))[0])

from string import*;from itertools import*;b=ascii_lowercase;s=r=input();t=s[::-1];d=0
if len(s)>25:d=-[p*len(list(k))for p,k in groupby(i<j for i,j in zip(t,t[1:]))][0]-2;s,b,r=s[:d],s[d+1:],b[:ord((s*2)[d])-97]
print(-(d<-26)or s+sorted(set(b)-set(r))[0])

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted