Problem:
 Score : 300 points    Problem Statement 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} .      Constraints  1 \leq |S| \leq 26  S is a diverse word.         Input Input is given from Standard Input in the following format:  S       Output Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.        Sample Input 1 atcoder      Sample Output 1 atcoderb  atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder . Note that atcoderb is lexicographically smaller than b .       Sample Input 2 abc      Sample Output 2 abcd       Sample Input 3 zyxwvutsrqponmlkjihgfedcba      Sample Output 3 -1  This is the lexicographically largest diverse word, so the answer is -1 .       Sample Input 4 abcdefghijklmnopqrstuvwzyx      Sample Output 4 abcdefghijklmnopqrstuvx

Solution (Python):
import copy
alpha=list("abcdefghijklmnopqrstuvwxyz")
alpha_dict={a:i for i,a in enumerate(alpha)}
alpha = set(alpha)

string = input()

can_use_str = alpha - set(list(string))

if string == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)

elif len(can_use_str)==0:
    k = 0
    for i in reversed(range(25)):
        if string[i] < string[i + 1]:
            k = i
            break
    last = sorted(string[k:])
    for i in last:
        if string[k] < i:
            print(string[:k] + i)
            break

else:
    string+=sorted(list(can_use_str))[0]
    print(string)

Solution (Python):
s=str(input())

p='abcdefghijklmnopqrstuvwxyz'
p=list(p)
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif len(s)!=26:
    for i in range(26):
        if p[i] not in s:
            print(s+p[i])
            break
elif len(s)==26:
    for i in range(24,-1,-1):
        if s[i]<s[i+1]:
            z=list(s[i+1:])
            z.sort()
            for j in range(len(z)):
                if s[i]<z[j]:
                    print(s[:i]+z[j])
                    break
            else:
                continue
            break

Solution (Python):
S = input()
character = list('abcdefghijklmnopqrstuvwxyz')

if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif len(S) < 26:
    for i in character:
        if i not in S:
            print(S + i)
            exit()
else:
    k = 0
    for i in reversed(range(25)):
        if S[i] < S[i + 1]:
            k = i
            break
    last = sorted(S[k:])
    for i in last:
        if S[k] < i:
            print(S[:k] + i)
            exit()

Solution (Python):
s=input()
alp="".join([chr(i) for i in range(97,97+26)])
if len(s)<26:
    for i in alp:
        if i not in s:
            print(s+i)
            exit()
else:
    if s=="".join([chr(i) for i in range(97,97+26)][::-1]):
        print(-1)
        exit()
    else:
        for i in range(24,-1,-1):
            for j in range(25,i,-1):
                if s[i]<s[j]:
                    print(s[:i]+s[j])
                    exit()

Solution (Python):
alp = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s) < 26:
  for x in alp:
    if x not in s:
      print(s+x)
      break
elif s == alp[::-1]:
  print(-1)
else:
  for i in range(24,-1,-1):
    for j in range(25,i,-1):
      if s[i] < s[j]:
        print(s[:i]+s[j])
        exit()

Solution (Python):
from collections import defaultdict
S = input()
alphabet = "abcdefghijklmnopqrstuvxyz"

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()

dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(S) < len(alphabet):
    for a in alphabet:
        if dict[a] == 0:
            print(S + a)
            exit()
else:
    for i in range(24, -1, -1):
        for j in range(25, i, -1):
            if S[i] < S[j]:
                print(S[:i] + S[j])
                exit()
print(-1)

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

s = input()

if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

L = 26
s = [ord(c) - ord('a') for c in s]

memo = [False] * L
for c in s:
    memo[c] = True

lowerbound = 0
while all(memo[lowerbound:]):
    c = s.pop()
    lowerbound = c + 1
    memo[c] = False

for i in range(lowerbound, L):
    if not memo[i]:
        s.append(i)
        break

print(''.join(map(chr, [c + ord('a') for c in s])))

Solution (Python):
s = input()
atoz = "abcdefghijklmnopqrstuvwxyz"
if len(s) < 26:
  for i in atoz:
    if i not in s:
      print(s+i)
      break
else:
  for i in range(len(s)):
    si = s[i:]
    if si == "".join(sorted(si, reverse=True)):
      if i == 0:
        print(-1)
        exit()
      else:
        hoge = s[i - 1]
        moge = list(si)
        moge.sort()
        for j in moge:
          if j > hoge:
            print(s[:i-1]+j)
            exit()

Solution (Python):

import sys

S = input()
alp = "abcdefghijklmnopqrstuvwxyz"
alpnum = {}

for i in range(26):
    alpnum[alp[i]] = i

dic = {}

for i in S:

    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

if len(dic) < 26:

    for i in range(26):

        if alp[i] not in dic:
            print (S+alp[i])
            break

else:

    now = alpnum[S[-1]]
    lis = []
    for i in range(26):

        if now > alpnum[S[-1]]:

            lis.sort()
            ans = ""
            for j in lis:

                if alpnum[j] > alpnum[S[-1]]:
                    ans = j
                    break
                    
            print ( S[:-1] + ans )
            sys.exit()
        else:
            lis.append(S[-1])
            now = alpnum[S[-1]]
            S = S[:-1]

    
    print (-1)

Solution (Python):
def f(s):
    
    return len(s) != len(set(s)) or s[-1] >= 123


S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit(0)
S_lis = list(map(ord, S))


S_lis += [97]

while f(S_lis):
    if S_lis[-1] == 123:
        S_lis.pop()
    S_lis[-1] += 1
print(''.join(map(chr, S_lis)))

Solution (Python):
import string

A = [chr(i) for i in range(ord('a'), ord('z')+1)]
B = {}
for i in range(26):
    B[A[i]] = i

s = input()
n = len(s)


def a():
    C = [0] * 26
    for i in range(n):
        C[B[s[i]]] = 1

    if n < 26:
        i = 0
        while C[i] == 1:
            i += 1
        return s + A[i]
    else:
        for i in range(25, -1, -1):
            t = s[i]
            for j in range(B[s[i]]+1, 26):
                if C[j] == 0:
                    return s[:i] + A[j]
            C[B[t]] = 0
            
            
        return -1

print(a())

Solution (Python):
S=input();n=len(S)
abc=[chr(i) for i in range(97,97+26)]

if n<26:
    s=set()
    for c in S:
        s.add(c)
    for c in abc:
        if c not in s:
            ans=S+c
            break
else:
    l=[]
    i=25
    l.append(S[i])
    while i>0 and S[i-1]>l[-1]:
        l.append(S[i-1])
        i-=1
    if i==0:
        ans='-1'
    else:
        c=S[i-1]
        l.append(c)
        l.sort()
        for j in range(len(l)):
            if l[j]==c:
                d=l[j+1]
        ans=S[:i-1]+d 

print(ans)

Solution (Python):
def f(s):
    return len(s) != len(set(s)) or s[-1] >= 123


st = input()
if st == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit(0)
s = list(map(ord, st))
s += [97]
while f(s):
    if s[-1] == 123:
        s.pop()
    s[-1] += 1
print(''.join(map(chr, s)))

Solution (Python):
s=input()
if len(s)==26:
	d=[]
	for i in range(26):
		if list(s[-i-1:][::-1])!=sorted(s[-i-1:]):
			for j in range(i+1):
				d.append(s[-j-1])
			d.sort()
			for j in range(len(d)):
				if d[j]>s[-i-1]:
					k=d[j]
					break
			print(s[:-i-1]+k)
			exit()
	print(-1)
	exit()
d=[0]*26
for i in range(len(s)):
	d[ord(s[i])-ord("a")]+=1
for i in range(26):
	if d[i]==0:
		x=chr(ord("a")+i)
		break
print(s+x)

Solution (Python):
s=input()
if len(s)==26:
	d=[]
	for i in range(26):
		if list(s[-i-1:][::-1])!=sorted(s[-i-1:]):
			for j in range(i+1):
				d.append(s[-j-1])
			d.sort()
			if d[0]==s[-i-1]:
				k=d[1]
			else:
				for j in range(len(d)):
					if d[j]>s[-i-1]:
						k=d[j]
						break
			print(s[:-i-1]+k)
			exit()
	print(-1)
	exit()
d=[0]*26
for i in range(len(s)):
	d[ord(s[i])-ord("a")]+=1
for i in range(26):
	if d[i]==0:
		x=chr(ord("a")+i)
		break
print(s+x)

Solution (Python):
import string
s = input()
lower = list(string.ascii_lowercase)

if len(s) < 26:
    for c in s: lower.remove(c)
    print(s + lower[0])
else:
    for i in range(24, -1, -1):
        if list(s[i:]) != sorted(s[i:])[::-1]:
            ans = s[:i]
            for j in range(1, 25):
                last = lower[lower.index(s[i])+j]
                if not last in ans:
                    ans += last
                    break
            print(ans)
            break
    else: print(-1)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()


l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

def main():
  s=S()
  if s=='zyxwvutsrqponmlkjihgfedcba':
    return -1

  for x in s:
    if l.count(x)==0:
      return -1
    l.remove(x)

  if len(l)>0:
    return ''.join(s)+l[0]

  nakama=[]
  for i in range(len(s))[::-1]:
    nakama.sort()
    b=s[i]
    for a in nakama:
      if a>b:
        return s[:i]+a
    nakama.append(b)


print(main())

Solution (Python):
import sys
S = input()
if len(S) < 26:
    for i in range(97, 97 + 26):
        if chr(i) not in S:
            print(S+chr(i))
            break
else:
    S_rev = S[::-1]
    for i in range(len(S_rev)-1):
        if S_rev[i] > S_rev[i+1]:
            for j in range(len(S_rev)):
                if S_rev[j] > S[-(i+2)]:
                    print(S[:len(S)-(i+1)-1]+S_rev[j])
                    sys.exit(0)
    print(-1)

Solution (Python):
S=input()
for c in"abcdefghijklmnopqrstuvwxyz":
 if(c in S)^1:print(S+c);quit()
for i in range(1,27):
 for c in sorted(S[-i:]):
  if c>S[-i]:print(S[:-i]+c);quit()
print(-1)

Solution (Python):
S=input()
N=len(S)
A="abcdefghijklmnopqrstuvwxyz"

if N<26:
    for a in A:
      if a not in S:
        S+=a
        break
else:
  if S==A[::-1]:
    S="-1"
  else:
    for i in range(N-1,-1,-1):
      s=S[i:]
      ss=list(S[i:])
      ss.sort(reverse=True)
      ss="".join(ss)
      
      if s!=ss:
        S=S[:i]+ss[ss.index(S[i])-1]
        break
print(S)

Solution (Python):
import string

S = input()
V = set(S)

for s in string.ascii_lowercase:
    if not s in V:
        print(S + s)
        exit()

for s, t in zip(S[:: -1], string.ascii_lowercase):
    if s != t:
        break
else:
    print(-1)
    exit()

for i in range(len(S))[:: -1]:
    A = []
    for j in range(i + 1, len(S)):
        if S[i] < S[j]:
            A.append(S[j])
    if A:
        A.sort()
        print(S[: i] + A[0])
        break

Solution (Python):
s = input()
n = len(s)
alphabet = [chr(ord("a")+i) for i in range(26)]
if n != 26:
    for i in alphabet:
        if i not in s:
            s += i
            break
    print(s)
else:
    for i in range(n-1, -1, -1):
        t = s[:i]
        n = alphabet.index(s[i])
        for j in range(n+1, 26):
            if alphabet[j] not in t:
                print(t+alphabet[j])
                exit()
    print(-1)

Solution (Python):
s=input()
N=len(s)
alpha="abcdefghijklmnopqrstuvwxyz"
a=[0]*26
for i in range(N):
    for j in range(26):
        if s[i]==alpha[j]:
            a[j]+=1
            break

if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
c=0
lastidx=-1
while True:
    for j in range(lastidx+1,26):
        if a[j]==0 :
            print(s[:N+c]+alpha[j])
            exit()
    lastidx=alpha.index(s[-1+c])
    a[lastidx] -= 1
    c-=1

Solution (Python):
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    if len(S) < 26:
        used = [0]*26
        for c in S:
            used[ord(c)-ord("a")] = 1
        print(S+chr(ord("a")+used.index(0)))
        return
    used = []
    while len(S)>0:
        c = S[-1]
        S = S[:-1]
        nxt = []
        for c2 in used:
            if c < c2:
                nxt.append(c2)
        if nxt:
            print(S+min(nxt))
            return
        used.append(c)
    print("-1")

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
s=input()
al=set(chr(ord('a')+i) for i in range(26))

def f(s,al):
    if len(s)==0:
        return -1
    t=f(s[1:],al-set([s[0]]))
    if t==(-1):
        larger=[x for x in al if x>s[0]]
        if not larger:
            return -1
        return min(larger)
    return s[0]+t

ans=f(s,al)
print(ans)

Solution (Python):
def main():
    S = str(input())
    sl =  list(S)
    if len(S) != 26:
        alpha = [i for i in range(26)]
        for i in sl:
            alpha.remove(ord(i)-97)
        n = min(alpha)
        print(S+chr(97+n))
    elif S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        alpha = []
        for i in range(25):
            alpha.append(S[-i-1])
            if ord(max(alpha)) > ord(S[-i-2]):
                alpha.sort()
                for j in alpha:
                    if ord(j) > ord(S[-i-2]):
                        print(S[:(-i-2)]+j)
                        exit()

if __name__ == "__main__":
    main()

Solution (Python):
import bisect

S = input()

N = len(S)
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif N < 26:
    t = [False] * 26
    for i in range(N):
        t[ord(S[i]) - 97] = True
    print(S + chr(t.index(False) + 97))
else:
    t = [S[-1]]
    for i in range(N - 2, -1, -1):
        if S[i] < S[i + 1]:
            break
        bisect.insort_left(t, S[i])
    print(S[:i] + t[bisect.bisect_left(t, S[i])])

Solution (Python):
S = input()

abc = list("abcdefghijklmnopqrstuvwxyz")

n = len(abc)
check = [0] * n
for s in S:
    check[ord(s) - 97] = 1

if S == ''.join(abc[::-1]):
    print(-1)
    exit()

if sum(check) != n:
    for i in range(n):
        if check[i] == 0:
            S += abc[i]
            break
    print(S)
else:
    check = [0] * n
    for i in range(0, n):
        check[ord(S[::-1][i]) - 97] = 1
        if S[::-1][i] > S[::-1][i + 1]:
            c = 24 - i
            break
    
    
    
    ans = S[:c]
    for i in range(ord(S[c]) - 97, n):
        if check[i] == 1:
            ans += abc[i]
            break
    print(ans)

Solution (Python):
S = input()
N = len(S)

D = [0 for i in range(26)]
for i in range(N):
    D[ord(S[i]) - 97] = 1

if N != 26:
    for i in range(26):
        if D[i] == 0:
            s = chr(97 + i)
            break
    print(S + s)

else:
    if S == "abcdefghijklmnopqrstuvwxyz"[::-1]:
        print(-1)
    else:
        c = 2
        for i in range(N):
            if ord(S[-2 - i]) - ord(S[-1 - i]) >= 1:
                c += 1
            else:
                break
        s = sorted(list(S[26 - c:]))
        for i in s:
            if ord(i) > ord(S[-c]):
                key = i
                break

        print(S[:26 - c] + key)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    s = list(input())
    n = len(s)
    alp = list("abcdefghijklmnopqrstuvwxyz")
    f = {}
    for i in range(26):
        f[alp[i]] = i
    if n == 26:
        if s == alp[::-1]:
            print(-1)
        else:
            d = [1]*26
            m = f[s[-1]]
            d[m] = 0
            for i in range(n-1)[::-1]:
                if m > f[s[i]]:
                    break
                m = f[s[i]]
                d[m] = 0
            k = f[s[i]]
            for j in range(k+1,n):
                if not d[j]:
                    break
            s = s[:i]+[alp[j]]
            print(*s,sep = "")
    else:
        d = [1]*26
        for i in s:
            d[f[i]] = 0
        for i in range(26):
            if d[i]:
                break
        s += [alp[i]]
        print(*s,sep = "")
    return

def B():

    return


def C():

    return


def D():

    return


def E():

    return


def F():

    return


if __name__ == "__main__":
    A()

Solution (Python):
s = input()
alphabet_small = [chr(i) for i in range(97, 97+26)]
avail = [True for i in range(26)]
for i in s:
    for j in range(26):
        if alphabet_small[j] == i:
            avail[j] = False
            break

for i in range(26):
    if avail[i]:
        print(s+alphabet_small[i])
        exit()

for i in range(25,-1,-1):
    alpha = s[i]
    k = -1
    for j in range(26):
        if alphabet_small[j] == alpha:
            avail[j] = True
            k = j
            break
    for j in range(26):
        if j > k and avail[j]:
            print(s[:i]+alphabet_small[j])
            exit()

print(-1)

Solution (Python):
def main():
    S = input()
    alpha = "abcdefghijklmnopqrstuvwxyz"
    alpha_rev = alpha[::-1]
    if S == alpha_rev:
        return -1
    Sset = set(list(S))
    if len(Sset) != 26:
        for c in alpha:
            if c not in Sset:
                return S + c
    else:
        for i in range(len(S)-1, 0, -1):
            if S[i] > S[i-1]:
                target = sorted(S[i-1:])
                for j in range(len(target)):
                    if target[j] > S[i-1]:
                        ref = target[j]
                        break
                t = S[:i-1] + ref
                return t
        return -1

if __name__ == '__main__':
    print(main())

Solution (Python):
from collections import defaultdict
S = input()
alphabet = "abcdefghijklmnopqrstuvxyz"

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()

dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(S) < len(alphabet):
    for a in alphabet:
        if dict[a] == 0:
            print(S + a)
            exit()
else:
    for i in range(24, -1, -1):
        for j in range(25, i, -1):
            if S[i] < S[j]:
                print(S[:i] + S[j])
                exit()
print(-1)

Solution (Python):

import sys, math, fractions, itertools, collections, copy, heapq


def solve(S: str):
    a = 'abcdefghijklmnopqrstuvwxyz'
    if len(S) != 26:
        for i in range(26):
            if not a[i] in S:
                print (*S, sep = '', end = '')
                print (a[i])
                exit()

    for i in range(25, -1, -1):
        if i == -1:
            break
        if ord(S[i]) < ord(S[i - 1]):
            continue
        else:
            break
    if i == 0:
        print (-1)
        exit()

    tmp_n = ord('z')
    for j in S[i:]:
        if ord(j) > ord(S[i - 1]):
            tmp_n = min(tmp_n, ord(j))

    print (*S[:i - 1], sep = '', end = '')
    print (chr(tmp_n))
 
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  
    solve(S)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()

def func(s, remain):
    if not s:
        if remain:
            return min(remain)
        else:
            return -1

    T = func(s[1:], remain - set([s[0]]))
    if T == -1:
        cand = [x for x in remain if x > s[0]]
        if cand:
            return min(cand)
        else:
            return -1
    return s[0] + T


rem = set()
for i in range(26):
    rem.add(chr(ord('a') + i))

print(func(S, rem))

Solution (Python):
import sys
input = sys.stdin.readline

S = input()

alphabets = set(chr(ord('a')+x) for x in range(26))

def F(S,alphabets):
    if len(S) == 0:
        return -1
    T = F(S[1:],alphabets-set([S[0]]))
    if T == -1:
        larger = [x for x in alphabets if x > S[0]]
        if not larger:
            return -1
        return min(larger)
    return S[0] + T

answer = F(S,alphabets)
print(answer)

Solution (Python):
S = list(input())

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, sep = '', end = '')
            print (a[i])
            exit()

for i in range(25, -1, -1):
    if i == -1:
        break
    if ord(S[i]) < ord(S[i - 1]):
        continue
    else:
        break

if i == 0:
    print (-1)
    exit()

tmp_n = ord('z')
for j in S[i:]:
    if ord(j) > ord(S[i - 1]):
        tmp_n = min(tmp_n, ord(j))

print (*S[:i - 1], sep = '', end = '')
print (chr(tmp_n))

Solution (Python):
def main():
    s = input().strip()
    if len(s) == 26:
        if s == "zyxwvutsrqponmlkjihgfedcba":
            print(-1)
            return
        s = s[::-1]
        pre = ord(s[0]) - ord("a")
        nums = [pre]
        for i in range(1, len(s)):
            tmp = ord(s[i]) - ord("a")
            if tmp < pre:
                min_ = pre
                for num in nums:
                    if num > tmp:
                        min_ = min(min_, num)
                s = s[i+1:]
                
                print(s[::-1]+chr(min_+ord("a")))
                return
            nums.append(tmp)
            pre = tmp
    else:
        check = [False] * 26
        for i in range(len(s)):
            check[ord(s[i]) - ord("a")] = True
        for i in range(26):
            if not check[i]:
                print(s+chr(ord("a")+i))
                return

if __name__ == "__main__":
    main()

Solution (Python):
*l,=map(ord,input()+'`')
for i in range(len(l)):
	p=l[:~i];s=set(range(l[~i]+1,123))-set(p)
	if s:print(*map(chr,p+[min(s)]),sep="");exit()
print(-1)

Solution (Python):
d=list("abcdefghijklmnopqrstuvwxyz")
s=input()
ss=list(s)
unused=list(set(d)^set(ss))
unused.sort()
if len(s)==26:
    cnt=1
    l=d.index(s[-1])
    for i in range(2,27):
        if d.index(s[-i])>l:
            l=d.index(s[-i])
            cnt+=1
        else:
            i=ss[-cnt-1]
            j=1
            while True:
                if d.index(i)>d.index(ss[-j]):
                    j+=1
                else:
                    break
            ans=ss[:-cnt-1]+[ss[-j]]
            print("".join(ans))
            exit()
    print(-1)
else:
    ans=ss+[unused[0]]
    print("".join(ans))

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

alpha = 'abcdefghijklmnopqrstuvwxyz'
s = input()
if s == alpha[::-1]:
    print(-1)
elif len(s) < 26:
    for i in alpha:
        if i not in s:
            print(s+i)
            break
else:
    temp=[]
    for i in range(25):
        temp.append(s[-i-1])
        if s[-i-2]<s[-i-1]:
            break
    for t in temp:
        if s[-i-2]<t:
            print(s[:-i-2]+t)
            break

Solution (Python):

alpha2num = lambda c: ord(c) - ord('a') + 1
num2alpha = lambda c: chr(c+64)
S = list(input())

S = S[::-1]
alpha = 'abcdefghijklmnopqrstuvwxyz'
alpha = list(alpha)

a = 0

if len(S) != 26:
    for i in range(len(alpha)):
        if alpha[i] not in S:
            b = alpha[i]
            break
    ans = ''.join(S)[::-1] + b
else:
    for i in range(1,len(S)):
        if S[i] < S[i-1]:
            a = i
            break
    if a == 0:
        ans = -1
    else:
        for i in range(len(alpha)):
            if alpha[i] not in S[a+1:] and alpha[i] > S[a]:
                c = alpha[i]
                break
        ans = (c + ''.join(S[a+1:]))[::-1]

print(ans)

Solution (Python):


s=input()
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

st=set(s)

if len(s)<26:
    for i in range(97,97+26+1):
        if chr(i) in st:continue
        print(s+chr(i))
        exit()

s=list(s)
res=[s.pop()]

def solve(s):
    for _ in range(len(s)):
        tmp=s.pop()
        if tmp>res[-1]:
            res.append(tmp)
            res.sort()
        else:            
            for e in res:
                if e<tmp:continue
                s.append(e)
                return s

print(''.join(solve(s)))

Solution (Python):
S0="zyxwvutsrqponmlkjihgfedcba"
S=input()
N=len(S)
if S==S0:
    print(-1)
elif len(S) == 26:
    for i in reversed(range(N-1)):
        if S[i]<S[i+1]:
            for j in reversed(range(i+1,N)):
                if S[j]>S[i]:
                    print(S[:i]+S[j])
                    exit()
else:
    for c in sorted(S0):
        if c not in S:
            print(S+c)
            exit()

Solution (Python):
import sys
input = sys.stdin.readline

S = input()

alphabets = set(chr(ord('a')+x) for x in range(26))

def F(S,alphabets):
    if len(S) == 0:
        return -1
    T = F(S[1:],alphabets-set([S[0]]))
    if T == -1:
        larger = [x for x in alphabets if x > S[0]]
        if not larger:
            return -1
        return min(larger)
    return S[0] + T

answer = F(S,alphabets)
print(answer)

Solution (Python):

import sys
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))



S = str(input())

alphabet = [chr(i) for i in range(ord('a'),ord('z')+1)]

if len(S)!=26:
  for i in range(len(S)):
    alphabet.remove(S[i])
  print(S+alphabet[0])
else:
  if S == ''.join(list(reversed(alphabet))):
    print(-1)
  else:
    for i in range(1,len(S))[::-1]:
      if S[i]>S[i-1]:
        break
    temp = 'z'
    for j in range(i,len(S)):
      if S[j]>S[i-1] and temp > S[j]:
        temp = S[j]
    print(S[:i-1]+temp)

Solution (Python):
S=list(input())
l=len(S)

available=[]
for i in range(26):
    available.append(chr(i+97))

for i in S:
    available.remove(i)

if l<26:
    S.append(available[0])
else:
    f=0
    for i in range(l):
        start=ord(S[-1])+1
        for i in range(26):
            want=chr(start+i)
            if want in available:
                S.remove(S[-1])
                S.append(want)
                f=1
                break
        if f==1:
            break
        else:
            available.append(S[-1])
            S.remove(S[-1])
if len(S)==0:
    ans='-1'
else:
    ans=''
    for i in S:
        ans+=i
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from copy import *

S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
atoz = [chr(i) for i in range(97,97+26)]
N = len(S)
if N == 26:
    for i in range(N-1)[::-1]:
        if S[i] < S[i+1]:
            print(S[:i]+min([y for y in S[i+1:] if y > S[i]]))
            break
else:
    for c in atoz:
        if not c in S:
            print(S+c)
            break

Solution (Python):

def main():
    S = input().rstrip() + "`"  
    for i in range(len(S)):
        P = S[:~i]  
        
        R = set(chr(i) for i in range(ord(S[~i]) + 1, 123)) - set(P)
        if R:
            print(P + min(R))
            break
    else:
        print(-1)


if __name__ == "__main__":
    main()

Solution (Python):
import string
def solve(s):
    if len(s) < 26: 
        for c in string.ascii_lowercase:
            if not c in s:
                return s + c
    
    i = len(s) - 2
    while i >= 0:
        for cand in sorted(s[i+1:]):
            if s[i] < cand:
                return s[:i] + cand
        i -= 1
    return -1

assert solve("atcoder") == "atcoderb"
assert solve("abc") == "abcd"
assert solve("zyxwvutsrqponmlkjihgfedcba") == -1
assert solve("abcdefghijklmnopqrstuvwzyx") == "abcdefghijklmnopqrstuvx"
s = input()
print(solve(s))

Solution (Python):
S = input()
alpha = "abcdefghijklmnopqrstuvwxyz"

if len(S) != 26:
    for i in alpha:
        if i not in S:
            print(S+i)
            exit()

for i in range(24, -1, -1):
    for j in range(25, i, -1):
        if S[i] < S[j]:
            print(S[:i]+S[j])
            exit()
print(-1)

Solution (Python):
s = input()
t = 'abcdefghijklmnopqrstuvwxyz'
if len(s) == len(t):
  for i in range(24,-1,-1):  
    for j in range(25,i,-1):
      if s[j] > s[i]:
        print(s[:i]+s[j])
        exit()
  print(-1)
  exit()
else:
  for i in t:
    if i not in s:
      print(s+i)
      exit()

Solution (Python):
S=input()

if S == 'zyxwvutsrqponmlkjihgfedcba':
    ans='-1'
else:
    if len(S)<26:
        for c in [chr(ord('a') + i) for i in range(26)]:
            if c not in S:
                ans = S+c
                break
    else:
        for i in range(25):
            if ord(S[25-i-1]) < ord(S[25-i]):
                for c in sorted(S[25-i:]):
                    if ord(S[24-i]) < ord(c):
                        ans=S[:24-i] + c
                        break
                break

print(ans)

Solution (Python):
S = input()

alpha2num = lambda c: ord(c) - ord('a') + 1
num2alpha = lambda c: chr(c+96)

snum = list(map(alpha2num, list(S)))

def next_snum(snum):
    if len(snum) <26:
        for i in range(1,27):
            if not i in snum:
                snum.append(i)
                return snum
    else:
        for i in range(25,0,-1):

            
            if snum[i-1] < snum[i]:
                
                
                minnum = 27
                for nxt in snum[i:]:
                    if minnum > nxt and nxt > snum[i-1]:
                        minnum = nxt
                        

                snum = snum[:i-1] + [minnum]

                return snum
        
        return -1

result_num = next_snum(snum)

if result_num == -1:
    print(-1)
else:
    print("".join(list(map(num2alpha,result_num))))