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 sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    if len(s)==26:
        for i in range(25,0,-1):
            if s[i]<s[i-1]:
                continue
            else:
                sx=s[i-1]
                for j in range(1,25):
                    sy=chr(ord(sx)+j)
                    if sy in s[i:]:
                        print(s[:i-1]+sy)
                        sys.exit()
        else:
            print(-1)
    else:
        for i in range(26):
            if chr(ord('a')+i) in s:
                continue
            print(s+chr(ord('a')+i))
            break

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    if len(s)==26:
        for i in range(25,0,-1):
            if s[i]<s[i-1]:
                continue
            else:
                sx=s[i-1]
                for j in range(1,25):
                    sy=chr(ord(sx)+j)
                    if sy in s[i:]:
                        print(s[:i-1]+sy)
                        sys.exit()
        else:
            print(-1)
    else:
        for i in range(26):
            if chr(ord('a')+i) in s:
                continue
            print(s+chr(ord('a')+i))
            break

if __name__=='__main__':
    main()

Solution (Python):
alf=[chr(ord('a') + i) for i in range(26)]

s=input()

for i in s:
    if i in alf:
        alf.remove(i)

if len(alf) > 0:
    print(s+alf[0])
else:
    if s=='zyxwvutsrqponmlkjihgfedcba':
        print(-1)
        exit()
    else:
        ans=[]
        for i in range(1,27):
            for j in ans:
                if s[-i]<j:
                    print(s[:26-i]+j)
                    exit()
            ans.append(s[-i])
            ans.sort()

Solution (Python):
S = str(input())
alp = "abcdefghijklmnopqrstuvwxyz"
s = len(S)
for i in range(s):
  alp = alp.replace(S[i],'')
if alp != '':
  print(S+alp[0])
  exit()
else:
  if S == "zyxwvutsrqponmlkjihgfedcba":
    S = "-1"
  else:
    for j in range(s-1,0,-1):
      if S[j] > S[j-1]:
        alp = list(S[j-1:])
        alp = sorted(alp)
        for k in range(len(alp)+1):
          if alp[k] == S[j-1]:
            S = S[:j-1]+alp[k+1]
            print(S)
            exit()
print(S)

Solution (Python):
s=input()

a2n = lambda c: ord(c) - ord('a') + 1

n2a = lambda c: chr(c+64).lower()

if len(s)<26:
  for i in range(1,27):
    if n2a(i) not in s:
      print(s+n2a(i))
      exit()
f=False
for i in range(1,26):
  if a2n(s[-i-1])>a2n(s[-i]):
    continue
  else:
    f=True
    break
if f==False:
  print(-1)
  exit()

ans=s[:-i-1]
mini=min([a2n(d) for d in s[-i:] if a2n(d)>a2n(s[-i-1])])
ans+=str(n2a(mini))
print(ans)

Solution (Python):
s=input()
res=[0]*26
for i in range(len(s)):
    res[ord(s[i])-97]=1
ans="-1"
for i in range(26):
    if res[i]==0:
        ans=s+chr(i+97)
        break
if ans=="-1":
    for i in range(26):
        for j in range(ord(s[25-i])-96,26):
            if res[j]==0:
                ans=s[:25-i]+chr(j+97)
                break
        if ans!="-1":
            break
        res[ord(s[25-i])-97]=0
print(ans)

Solution (Python):
s = input()

a = list('abcdefghijklmnopqrstuvwxyz')

if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for x in a:
        if s.count(x) == 0:
            print(s + x)
            break
else:
    end = False
    for i in range(25)[::-1]:
        if end:
            break
        for j in range(i+1,26)[::-1]:
            if s[i] < s[j]:
                print(s[:i]+s[j])
                end = True
                break

Solution (Python):
S = input()

if len(S) != 26:
    chars = set()
    for s in S:
        chars.add(s)
    for c in range(ord('a'), ord('a') + 26):
        if chr(c) not in chars:
            print(S + chr(c))
            exit(0)


ans = S[0:25]
S = [ord(s) for s in S]
prev = [S[25]]
for i in range(24, -1, -1):
    res = 10000
    for p in prev:
        if S[i] < p:
            res = min(res, p)
    if res == 10000:
        prev.append(S[i])
    else:
        print(ans[0:i] + chr(res))
        exit(0)

print(-1)

Solution (Python):


import sys
import bisect
li = [chr(i) for i in range(97, 97+26)]
s = input()
if len(s)<26:
    for l in li:
        if l not in s:
            print(s+l)
            sys.exit()
elif s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    r = s[::-1]
    tmp = ''
    kabe = ''
    for i,rr in enumerate(r):
        if i == 0:
            continue
        if rr < r[i-1]:
            tmp = r[:i+1][::-1]
            kabe = rr
            break
    

    tmp_li = list(tmp)
    tmp_li.sort()
    ind = bisect.bisect_left(tmp_li, kabe)
    s0 = tmp_li[ind+1]
    
    print(s.replace(tmp, s0))

Solution (Python):
S=input()
s=[]
for i in range(26):
  s=s+[chr(97+i)]
for i in range(len(S)):
  s.remove(S[i])
if len(s)==0:
  for i in range(25):
    if ord(S[24-i])<ord(S[25-i]):
      q=S[25-i:]
      q2=[]
      for j in q:
        if ord(j)<ord(S[24-i]):
          pass
        else:
          q2+=[ord(j)]
      q2.sort()
      
      S=S[:24-i]+chr(q2[0])
      print(S)
      break
    if i==24:
      print(-1)
    
else:
  print(S+s[0])

Solution (Python):
S=input()
s=[]
for i in range(26):
  s=s+[chr(97+i)]
for i in range(len(S)):
  s.remove(S[i])
if len(s)==0:
  for i in range(25):
    if ord(S[24-i])<ord(S[25-i]):
      q=S[25-i:]
      q2=[]
      for j in q:
        if ord(j)<ord(S[24-i]):
          pass
        else:
          q2+=[ord(j)]
      q2.sort()
      
      S=S[:24-i]+chr(q2[0])
      print(S)
      break
    if i==24:
      print(-1)
    
else:
  print(S+s[0])

Solution (Python):
alp=[chr(ord('a') + i) for i in range(26)]
unused=set(alp)
     
S=input()


if len(S)<26:
    for i in range(len(S)):
        if S[i] in unused:
            unused.remove(S[i])
    unused=sorted(unused)
    print(S+unused[0])
else:
    for j in range(len(S)-1,-1,-1):
        p=set(S[j:])
        
        q=sorted(p)
        if S[j]==q[-1]:
            pass
        else:
            idx=q.index(S[j])
            print(S[:j]+q[idx+1])
            break
    else:
        print(-1)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    S = input()
    A = set(chr(ord("a") + x) for x in range(26))
    if len(S) < 26:
        se = A - set(S)
        x = min(se)
        print(S + x)

    else:
        if S == "".join(sorted(A, reverse=True)):
            print(-1)
            return
        else:
            c = []
            S = list(S)
            while True:
                x = S[-1]
                if not c or (x > max(c)):
                    c.append(S.pop())
                    continue
                y = min(ch for ch in c if ch > x)
                S = "".join(S[:-1]) + y
                print(S)
                return


if __name__ == "__main__":
    main()

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return "".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

s = list(input())[:-1]

ans = ""

po = [0 for i in range(26)]

if jo(s) == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()

if len(s) == 26:
    for i in range(25,-1,-1):
        if i == 25:
            po[ord(s[-1])-97] = 1
            continue
        else:
            if s[i] > s[i+1]:
                po[ord(s[i])-97] = 1
                po[ord(s[i+1])-97] = 1
            else:
                ans = jo(s[:i])
                for j in range(26):
                    if po[j] and ord(s[i])-97 < j:
                        ans += chr(97+j)
                        break
                

                break


else:
    for i in range(len(s)):
        on = ord(s[i])-97
        if po[on] == 0:
            ans += s[i]
            po[on] = 1
    for j in range(26):
        if po[j] == 0:
            ans += chr(97+j)
            break

print(ans)

Solution (Python):
S = input()
if len(S) == 26:
    last = None
    l = [False for i in range(26)]
    for i, s in enumerate(S):
        l[ord(s)-ord('a')] = True
        o = ord(s)-ord('a')
        while o <= ord('z')-ord('a'):
            if l[o] == False:
                last = S[:i] + chr(ord('a') + o)
                break
            o += 1
    if last:
        print(last)
    else:
        print(-1)
else:
    l = [False for i in range(26)]
    for s in S:
        l[ord(s)-ord('a')] = True
    for i in range(26):
        if l[i] == False:
            print(S+chr(ord('a') + i))
            break

Solution (Python):
import collections
S = input()

if(S == "zyxwvutsrqponmlkjihgfedcba"):
    print(-1)
    exit()
    
alp = "abcdefghijklmnopqrstuvwxyz"
r = S[::-1]

c = collections.Counter(S)
c_s = sorted(c.items(), key=lambda x:x[0])
tmp = ""

for i in range(len(c_s)):
    tmp += c_s[i][0]

if(len(S) < 26):
    for i in range(len(alp)):
        if(alp[i] not in tmp):
            print(S+alp[i])
            break
else:
    for i in range(len(S)-1):
        if(r[i] > r[i+1]):
            cha = sorted(r[:(i+1)])
            for j in range(len(cha)):
                if(cha[j] > r[i+1]):
                    print(S[:(len(S)-i-2)]+cha[j])
                    exit()

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

S = input()

if S == "zyxwvutsrqponmlkjihgfedcba":
	print(-1)
elif len(S) < len(ascii_lowercase):
	for char in ascii_lowercase:
		if char not in S:
			print(S+char)
			break
else:
	S = S[::-1]
	for i in range(len(S)-1):
		if S[i+1] < S[i]:
			for char in sorted(S[:i+1]):
				if char > S[i+1]:
					print(S[::-1][:-i-2]+char)
					exit()

Solution (Python):
s = input()
c = list(s)
c2 = set(c)
al = sorted(list("qwertyuiopasdfghjklzxcvbnm"))


if len(c)<26:
    for i in range(26):
        if al[i] not in c2:
            print(s+al[i])
            exit()

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

rev = "zyxwvutsrqponmlkjihgfedcba"
for i in range(25,-1,-1):
    x = sorted(c[i:])
    for j in x:
        if ord(s[i])<ord(j):
            print(s[:i]+j)
            exit()

Solution (Python):
def main():
    S = input()
    S_set = set(list(S))
    C_set = set([chr(ord("a") + i) for i in range(26)])

    if len(S_set) == len(C_set):
        S = list(S)
        p = None
        for i in range(len(S) - 2, -1, -1):
            if S[i] < S[i + 1]:
                p = i
                break
        else:
            print(-1)
            return

        ans = S[:p].copy()
        rest = C_set - set(S[: p + 1])
        rest = sorted(rest)
        for r in rest:
            if S[p] < r:
                ans.append(r)
                break
        print(*ans, sep="")
    else:
        diff = C_set - S_set
        diff = sorted(diff)
        print(S + diff[0])
        return


main()

Solution (Python):


from string import ascii_lowercase

s = input()
rems = [c for c in ascii_lowercase if c not in s]

if len(rems) > 0:
    print("{}{}".format(s, rems[0]))
    exit()
else:
    
    

    
    j = len(s) - 1
    while s[j] < s[j - 1]:
        j -= 1

    if j == 0:
        print(-1)
    else:
        sj = s[:j - 1]
        pjm1 = s[j - 1]
        nc = min([c for c in s[j:] if c > pjm1])
        print(sj + nc)

Solution (Python):
s = list(input())
alph = [chr(i) for i in range(97, 123)]
alph_count = [0] * 26
for i in s:
    alph_count[alph.index(i)] = 1
ans = ["-1"]
if len(s) != 26:
    for i in range(26):
        if alph_count[i] == 0:
            ans = s + [alph[i]]
            break
else:
    for i in range(25, -1, -1):
        c = -1
        for j in range(26):
            if s[i] == alph[j]:
                c = j
            if not c == -1:
                if alph_count[j] == 0:
                    s[i] = alph[j]
                    ans = s[0 : i + 1]
            if not ans[0] == "-1":
                break
        alph_count[c] = 0
        if not ans[0] == "-1":
            break
print("".join(ans))

Solution (Python):
S = list(input())
N = len(S)
if N == 26:
    if S == list("zyxwvutsrqponmlkjihgfedcba"):
        print(-1)
    else:
        O = [0]*(N+1)
        for i in range(N):
            O[i] = ord(S[i])

        for i in range(N)[::-1]:
            co = [0]*N
            
            for j in range(1,25):

                for k in range(N):
                    co[k] = O[k]

                co[i] += j

                if co[i] > 122:
                    continue
                else:
                    if len(set(co[0:i+1])) == len(co[0:i+1]):
                        ans = co[0:i+1]
                        break

            else:
                continue
            break

        for i in range(len(ans)):
            ans[i] = chr(ans[i])

        cans = "".join(ans)
        print(cans)

else:
    
    O = [0]*(N+1)
    for i in range(N):
        O[i] = ord(S[i])

    for i in range(97,123):
        if i not in O:
            last = i
            break 
    
    O[N] = i

    for i in range(N+1):
        O[i] = chr(O[i])
    
    ans = "".join(O)
    print(ans)

Solution (Python):
S = list(input())
l = len(S)
if S == list('zyxwvutsrqponmlkjihgfedcba'):
    print(-1)
else:
    if l != 26:
        for i in range(26):
            if not chr(i+97) in S:
                S.append(chr(i+97))
                break
        print(''.join(S))
    else:
        li = []
        for i in range(26):
            c = S.pop()
            li.append(c)
            for x in li:
                if x>c:
                    S.append(x)
                    print(''.join(S))
                    break
            else:
                continue
            break

Solution (Python):


s = str(input())


alphabet = set(chr(ord('a')+i) for i in range(26))

def F_short(s):
  
  se = alphabet - set(s)
  x = min(se)
  return s + x

def F_long(s):
  
  if s == ''.join(sorted(alphabet, reverse=True)):
    return -1

  
  cand = []  
  s = list(s)
  while True:
    x = s[-1]  
    if (not cand) or (x > max(cand)):
      cand.append(s.pop())
      continue
    else:
      break
  y = min(ch for ch in cand if ch > x)
  return ''.join(s[:-1]) + y

if len(s) < 26:
  ans = F_short(s)
else:
  ans = F_long(s)
print(ans)

Solution (Python):
s = str(input())
ans = ''
if len(s) < 26:
  for i in range(ord('a'), ord('z')+1):
    if chr(i) not in s:
      ans = s + chr(i)
      break
else:
  for i in range(24, -1, -1):
    if ord(s[i]) < ord(s[i+1]):
      ans = s[:i]
      for j in range(ord(s[i])+1, ord('z')+1):
        if chr(j) not in ans:
          ans += chr(j)
          break
      break
if ans: print(ans)
else: print(-1)

Solution (Python):



from itertools import permutations
S=input()
N=len(S)
alp=[0]*26
searched=[0]*26
flag=0
c=0
sub=100
minsub=100

for i in range(N):
    alp[ord(S[i])-97]=alp[ord(S[i])-97]+1
    
if (max(alp)!=1)or(S=="zyxwvutsrqponmlkjihgfedcba"):
    flag=0
else:
    if N!=26:
        for i in range(26):
            if alp[i]==0:
                c=chr(97+i)
                S="".join((S,c))
                flag=1
                break
    else:
        i=len(S)-2
        s=S[i:]
        
        while True:
            piv=s[0]
            
            for j in range(len(s)-1):
                if ord(s[j+1])>ord(piv):
                    sub=ord(s[j+1])-ord(piv)
                    
                    if sub<minsub:
                        minsub=sub
            if (minsub!=100):
                
                S="".join((S[:i],chr(ord(piv)+minsub)))
                flag=1
                break

            minsub=100
            sub=0
            i=i-1
            s=S[i:]

if flag==0:
    print("-1")
else:
    print(S)

Solution (Python):
s = input()
list_s = list(s)
len_s = len(list_s)
check = ['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']
for i in range(26):
    if check[i] not in list_s:
        print(s + check[i])
        exit()
for i in range(1, len_s + 1):
    memo = list_s.pop(len_s - i)
    for j in range(check.index(memo) + 1, 26):
        if check[j] not in list_s:
            print(s[:26 - i] + check[j])
            exit()
print(-1)

Solution (Python):
s = input()
s_origin = s
n = len(s)

for i in range(n):
    for j in range(ord("a"), ord("z")+1):
        if not (chr(j) in s) and \
            s + chr(j) != s_origin and \
                s_origin < s + chr(j):
            print(s + chr(j))
            exit()
    if len(s) == 1 and s != "z":
        print(chr(ord(s) + 1))
        exit()
    s = s[:-1]

print(-1)

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

if len(S) < 26:
    s = set(list(S))
    for i in range(26):
        if alpha[i] not in s:
            S += alpha[i]
            break
else:
    flag = False
    for i in range(24, -1, -1):
        tmp = ord(S[i])
        for j in range(25, i, -1):
            if tmp < ord(S[j]):
                S = S[:i] + S[j]
                flag = True
                break    
        
        if flag:
            break
    else:
        S = "-1"

print(S)

Solution (Python):
s = input()
alf = 'abcdefghijklmnopqrstuvwxyz'
pos = {}
pos_alf = {c:i for i,c in enumerate(alf)}

for i,c in enumerate(s):
    if c not in pos:
        pos[c] = []
    pos[c] = i





if len(s) != 26:
    for c in alf:
        if c not in pos:
            print(s+c)
            exit()

tmp = [pos_alf[s[-1]]]
for i in range(len(s)-1)[::-1]:
    if pos_alf[s[i]] < max(tmp):
        d = [j for j in tmp if j > pos_alf[s[i]]]
        print(s[:i]+alf[min(d)])
        break
    else:
        tmp.append(pos_alf[s[i]])
    
else:
    print(-1)

Solution (Python):
s = input()

if len(s)<26:
        st = set(s)
        for c in "abcdefghijklmnopqrstuvwxyz":
                if not c in st:
                        print(s+c)
                        break
        else: print(-1)
else:
        prev = ord('a') - 1
        i = len(s)
        for c in s[::-1]:
                if ord(c)>prev: i -= 1
                else: break
                prev = ord(c)
        if i==0: print(-1)
        else:
                for c in sorted(s[i-1:]):
                        if ord(c)>ord(s[i-1]):
                                print(s[:i-1] + c)
                                break

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


if __name__ == '__main__':
    main()

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

import string
def f(x, mn=None):
    for c in string.ascii_lowercase:
        if c in x: continue
        if mn and c <= mn: continue
        return c
    return None

if n < 26:
    print(S + f(S))
    exit()

i = n-2
while i >= 0:
    if S[i] < S[i+1]: break
    i -= 1
else:
    print(-1)
    exit()

t = S[:i]
print(t + f(t, S[i]))

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)
    else:
        for i in range(25)[::-1]:
            for j in range(i+1,26)[::-1]:
                if s[i]<s[j]:
                    print(s[:i]+s[j]);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)
    else:
        for i in range(25)[::-1]:
            for j in range(i+1,26)[::-1]:
                if s[i]<s[j]:
                    print(s[:i]+s[j])
                    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(25)[::-1]:
            for j in range(i+1,26)[::-1]:
                if s[i]<s[j]:
                    print(s[:i]+s[j])
                    exit()

Solution (Python):
import string
S = input()
alph = string.ascii_lowercase

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

if set(S) == set(alph):
    L = []
    for i in range(len(S) - 1):
        T = S[:i]
        for a in alph:
            if not a in T and (T + a) > S[:i + 1]:
                L.append(T + a)
                break
    L.sort()
    print(L[0])
else:
    for a in alph:
        if not a in S:
            print(S + a)
            exit()

Solution (Python):
def main():
    S = input()
    from string import ascii_lowercase
    dic = {a: i for i, a in enumerate(ascii_lowercase)}
    rdic = {i: a for i, a in enumerate(ascii_lowercase)}
    if len(S) != 26:
        for s in ascii_lowercase:
            if s not in S:
                return print(S + s)
    elif S == "zyxwvutsrqponmlkjihgfedcba":
        return print(-1)

    pre = S[-1]
    mi = [pre]
    for i, s in enumerate(S[-2::-1], start=2):
        if s < pre:
            
            
            
            
            m = min(mi, key=lambda p: s > p)
            return print(S[:-i] + m)
        pre = s
        mi.append(s)


if __name__ == '__main__':
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
if len(s) < 26:
    for i in range(26):
        tmp = chr(i + ord('a'))
        if not tmp in s:
            print(s + tmp)
            exit()
else:
    for i in reversed(range(26)):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                print(s[:i] + chr(j))
                exit()
    print(-1)
    exit()

Solution (Python):

import copy
s = list(input())

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

if len(s) == 26:
    for i in reversed(range(0, 26)):
        
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                s[i] = chr(j)
                print(''.join(s[:i + 1]))
                exit()

for i in range(26):
    if not chr(ord('a') + i) in s:
        s.append(chr(ord('a') + i))
        break
ans2 = ''.join(s)
print(ans2)

Solution (Python):
def main():
    S = input()
    from string import ascii_lowercase
    dic = {a: i for i, a in enumerate(ascii_lowercase)}
    rdic = {i: a for i, a in enumerate(ascii_lowercase)}
    if len(S) != 26:
        for s in ascii_lowercase:
            if s not in S:
                return print(S + s)
    elif S == "zyxwvutsrqponmlkjihgfedcba":
        return print(-1)

    pre = S[-1]
    mi = [pre]
    for i, s in enumerate(S[-2::-1], start=2):
        if s < pre:
            m = min(mi, key=lambda p: s > p)
            return print(S[:-i] + m)
        pre = s
        mi.append(s)


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
alphabets = set(chr(ord("a") + x) for x in range(26))

def F_short(S):
  se = alphabets - set(S)
  x = min(se)
  return S + x

def F_long(S):
  if S == "".join(sorted(alphabets, reverse=True)):
    return "-1"
  
  cand = []
  S = list(S)
  while True:
    x = S[-1]
    if not cand or (x > max(cand)):
      cand.append(S.pop())
      continue
    y = min(ch for ch in cand if ch > x)
    return "".join(S[:-1]) + y

L = len(S)
ans = F_short(S) if L < 26 else F_long(S)
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
if len(s) < 26:
    for i in range(26):
        tmp = chr(i + ord('a'))
        if not tmp in s:
            print(s + tmp)
            exit()
if len(set(s)) == 26:
    for i in reversed(range(len(s))):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                print(s[:i] + chr(j))
                exit()
    print(-1)
    exit()

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

if len(S) < 26:
    for i in range(26):
        if not chr(ord("a") + i) in S:
            print(S + chr(ord("a") + i))
            exit()
else:
    pop_list = []
    S = list(S)
    while True:
        pop_chr = S.pop()
        pop_list.append(pop_chr)
        pop_list.sort()
        for candidate in pop_list:
            if S[-1] < candidate:
                S.pop()
                S.append(candidate)
                print(*S, sep="")
                exit()

Solution (Python):
sa = input()
s = list(sa)
a = [chr(ord('a') + i) for i in range(26)]
if sa == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    for i in range(len(a)):
        if a[i] not in s:
            s.append(a[i])
            print("".join(s))
            exit()
else:
    used = [s[25]]
    for i in range(25, 0, -1):
        if a.index(s[i-1]) > a.index(s[i]):
            used.append(s[i-1])
        else:
            for j in range(a.index(s[i-1])+1, 26):
                if a[j] in used:
                    s[i-1] = a[j]
                    print("".join(s[:i]))
                    exit()

Solution (Python):
s = input()
l = len(s)
backet = [0] * 26
if l != 26:
  for i in s:
    backet[ord(i) - 97] = 1
  for i in range(26):
    if not backet[i]:
      print(s + chr(i + 97))
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    d = []
    for i in s[::-1]:
      s = s[:-1]
      d.sort()
      for j in d:
        if j > ord(i)-97:
          print(s + chr(j+97))
          exit()
      d.append(ord(i)-97)

Solution (Python):

import sys
input = sys.stdin.readline
from collections import Counter

s = input().rstrip()
n = len(s)
cnt = Counter(s)

for i in range(97, 97 + 26):
    key = chr(i)
    if key not in s:
        print(s + key)
        exit()

largest = s[-1]
seen = []
for i, ch in enumerate(s[::-1]):
    if ch < largest:
        for item in seen:
            if ch < item:
                s = s[:n-i-1] + item
                print(s)
                exit()
    largest = max(largest, ch)
    seen.append(ch)
    seen.sort()

print(-1)