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):
from sys import stdin
import string
import numpy as np

S = (stdin.readline().rstrip())
remain = string.ascii_lowercase

if S == remain[::-1]:
    print(-1)

else:

    for i in S:
        remain = remain.replace(i, "")

    if remain != "":
        ans = S + sorted(remain)[0]
        print(ans)

    else:
        S = np.array(list(S))
        for i in range(25, 0, -1):
            S_left = S[:i]
            S_right = np.sort(S[i:])
            if len(S_right[S_right > S_left[-1]]) != 0:
                ans = list(S[:i-1]) + list(S_right[S_right > S_left[-1]][0])
                ans = "".join(ans)
                print(ans)
                break

Solution (Python):
s=input()
lens=len(s)
if lens!=26:
    for i in "abcdefghijklmnopqrstuvwxyz":
        if i not in s:
            print(s+i)
            break
elif s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
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):
import string

S = input()

abc = [chr(i) for i in range(97, 97+26)]

if len(S) < 26:
    for s in S:
        abc.remove(s)
    add = abc[0]
    print(S + add)
elif S == ''.join(sorted(abc, reverse=True)):
    print(-1)
else:
    N = len(S)
    for i in range(1, N):
        now = S[N-i-1]
        fow = S[N-i:]
        fows = sorted(list(fow))
        for f in fows:
            if now < f:
                now = f
                break
        if now != S[N-i-1]:
            ans = S[:N-i-1] + now
            break
    print(ans)

Solution (Python):
s=input()
t="abcdefghijklmnopqrstuvwxyz"
if s==t[::-1]:print(-1);exit()
if len(s)<26:
  for i in t:
    if not i in s:print(s+i);exit()
else:
  for i in range(25):
    if s[-i-2]<s[-1-i]:
      t=s[:-i-2]
      a=sorted(list(s[-2-i:]))
      print(t+a[a.index(s[-2-i])+1]);exit()

Solution (Python):
s=input()

ll=["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"]

if len(s)!=26:
    l=list(s)
    for i in l:
        ll.remove(i)
    ll.sort()
    print(s+ll[0])
else:
    if s=="zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        l=[]
        for i in range(1,27):
            for j in l:
                if s[-i]<j:
                    print(s[0:26-i]+j)
                    import sys
                    sys.exit()
            l.append(s[-i])
            l.sort()

Solution (Python):
s=input()

ll=["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"]

if len(s)!=26:
    l=list(s)
    for i in l:
        ll.remove(i)
    ll.sort()
    print(s+ll[0])
else:
    if s=="zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        l=[]
        for i in range(1,27):
            for j in l:
                if s[-i]<j:
                    print(s[0:26-i]+j)
                    import sys
                    sys.exit()
            l.append(s[-i])
            l.sort()

Solution (Python):

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
import heapq
INF = float("inf")
sys.setrecursionlimit(10**7)

dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def mi(): return map(int, input().split())
def ii(): return int(input())


S = input()

end = "abcdefghijklmnopqrstuvwxyz"[::-1]

if S == end:
  print(-1)
  exit()

lastChar = S[-1]
set_used = set(S)

if len(S)<len(end): 
  for i in range(ord('a'), ord('z')+1):
    if not chr(i) in set_used:
      print(S + chr(i))
      exit()
else: 
  prevord = -1
  for i, c in enumerate(S[::-1]): 
    if ord(c)<prevord: 
      
      ansS_exceptLast = S[0:-i-1]
      for cc in range(ord(c)+1, ord('z')+1):
        if not chr(cc) in ansS_exceptLast:
          print(ansS_exceptLast+chr(cc))
          exit()
    else:
      prevord = ord(c)

Solution (Python):
wrd = [chr(i+97) for i in range(26)]
S = input()
ans = ''
for s in S:
    wrd.remove(s)
wrd.sort()
if(S=='zyxwvutsrqponmlkjihgfedcba'):
    print(-1)
elif(len(S)<26):
    print(S+wrd[0])
else:
    chrs = []
    for i in range(len(S)-1,-1,-1):
        for j in chrs:
            if(j>S[i]):
                ans = S[:i]+j
                break
        else:
            chrs.append(S[i])
            chrs.sort()
            continue
        break

    print(ans)

Solution (Python):
s=input()
a='abcdefghijklmnopqrstuvwxyz'
r='zyxwvutsrqponmlkjihgfedcba'
if len(s)!=26:
  for i in a:
    if i not in s:
      s=s+i
      break
elif s==r:
  s=-1
else:
  for i in range(25,0,-1):
    if i==1:
      s=chr(ord(s[0])+1)
      break
    if s[i]>s[i-1]:
      l=sorted(s[i:])
      for j in l:
        if j>s[i-1]:
          m=j
          break
      s=s[:i-1]+m
      break
print(s)

Solution (Python):
def main():
    S = input()
    if len(S) < 26:
        exists = set()
        for c in S:
            exists.add(c)
        for i in range(26):
            x = chr(ord('a') + i)
            if x not in exists:
                print(S + x)
                return
    seen = []
    for i in range(25, -1, -1):
        last = None
        for x in seen:
            if S[i] < x:
                if last == None or x < last:
                    last = x
        if last:
            print(S[:i] + last)
            return
        seen.append(S[i])
    print(-1)


if __name__ == "__main__":
    main()

Solution (Python):
S = list(input())
S = [ord(x) for x in S]
S_exist = sorted(S)
if len(S_exist) != 26:
    for i in range(len(S_exist)):
        if S_exist[i] != i+97:
            S.append(i+97)
            break
    if len(S_exist) == len(S):
        S.append(max(S_exist)+1)
    print("".join(map(str, [chr(x) for x in S])))
else:
    S.reverse()
    if S == S_exist:
        print(-1)
    else:
        flag = 0
        for i in range(25):
            if S[i] > S[i+1]:
                a = S[i+1]
                for j in sorted(S[:i+2]):
                    if j > a:
                        S = [j] + S[i+2:]
                        flag = 1
                        break
            if flag == 1:
                break
        S.reverse()
        print("".join(map(str, [chr(x) for x in S])))

Solution (Python):
S = input()
alpha = 'abcdefghijklmnopqrstuvwxyz'
if S == alpha[::-1]:
    print(-1)
else:
    rest = sorted(list(set(alpha) - set(S)))
    if rest:
        print(S + rest[0])
    else:
        for i in range(len(S)-1, 0, -1):
            if S[i-1] < S[i]:
                break
        rest = sorted([s for s in S[i:] if s > S[i-1]])
        print(S[:i-1] + rest[0])

Solution (Python):
s = str(input())
alpha = "abcdefghijklmnopqrstuvwxyz"
if len(s) < 26:
    toappend = ""
    for i in alpha:
        if i not in s:
            toappend = i
            break
    s += toappend
    print(s)
else:
    if s == alpha[::-1]:
        print(-1)
    else:
        s = s[::-1]
        ckd = []
        for i in range(26):
            for j, it in enumerate(ckd):
                if s[i] < it:
                    temp = ckd[j] + s[i+1:]
                    print(temp[::-1])
                    exit()
            ckd.append(s[i])

Solution (Python):
S=input()
Sf = S+" "

Flag = True
for i in range(len(S)*2):
  se = set(S)
  p = -1
  for i in range(26):
    if chr(ord('a')+i) not in se and ord('a')+i > ord(Sf[len(S)]):
      p= i
      break
    p = -1
  
  if (p == -1):
    if ( len(S)==0 ):
        Flag = False
        break
    S = S[:-1]
  else:
    break
    
if ( Flag ):
  print ( S + chr(ord('a')+p) ) 
else:
  print ( -1 )

Solution (Python):
s = input().rstrip()
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) == 26:
    s = s[::-1]
    seen = []
    for i in range(26):
        for j, item in enumerate(seen):
            if s[i] < item:
                ns = seen[j] + s[i+1:]
                print(ns[::-1])
                exit()
        seen.append(s[i])
else:
    alphabets = set(s)
    for item in "abcdefghijklmnopqrstuvwxyz":
        if item not in alphabets:
            print(s + item)
            exit()

Solution (Python):

"""
A - Diverse Word
https://atcoder.jp/contests/agc022/tasks/agc022_a

"""
import sys


from string import ascii_lowercase

def solve(S):
    if len(S) < len(ascii_lowercase):
        remaining = sorted(set(ascii_lowercase) - set(S))
        return S + remaining[0]
    for p in range(len(S)-1, -1, -1):
        pre = S[:p]
        ch = chr(ord(S[p]) + 1)
        while 'a' <= ch <= 'z':
            if ch not in pre:
                return pre + ch
            ch = chr(ord(ch) + 1)
    return -1


def main(args):
    S = input()
    ans = solve(S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import collections
S = input()
alp = 'abcdefghijklmnopqrstuvwxyz'
dic = collections.defaultdict(int)
ans = -1
if len(S) <= 25:
    for i in S:
        dic[i] += 1
    for i in alp:
        if dic[i] == 0:
            ans = S + i
            break
else:
    dic2 = {}
    dic3 = {}
    lst = []
    tmp = 0
    flag = False
    for i, j in zip(alp, range(1, 27)):
        dic2[i] = j
        dic3[j] = i
    for i in range(1, 27):
        a2 = S[-i]
        idx = dic2[a2]
        if idx < tmp:
            lst.sort()
            for j in lst:
                if j > idx:
                    ans = S[:-i] + dic3[j]
                    flag = True
                    break
        
        else:
            tmp = idx
            lst.append(idx)
        if flag:
            break

print(ans)

Solution (Python):
import itertools
import string
n=26
alp=string.ascii_lowercase[0:n]

def nextchar(c):
    if c==alp[n-1]:
        return '0'
    z=alp
    return z[alp.find(c)+1]
    
def nextword(c):
    if c==alp[::-1]:
        return -1
    else:
        while True:
            c=c[:len(c)-1]
            l=nextchar(c[-1])
            while l!='0':
                if l not in c:
                    return c[:len(c)-1]+l
                l=nextchar(l)

s=input()
if len(s)<n:
        for i in alp:
            if i not in s:
                print(s+i)
                break
else:
    print(nextword(s))

Solution (Python):
S=input()
final=(''.join([ chr(97+i) for i in range(26)]))[::-1]
if S==final:
	print('-1')
else:
	if len(S)<26:
		for i in range(26):
			if chr(97+i) in S:
				continue
			else:
				print(S+chr(97+i))
				break
	else:
		S_cut = S
		continuate = True
		while continuate:
			S_cut = S_cut[:-1]
			tail = ord(S_cut[-1]) - ord('a')
			for i in range(tail+1, 26):
				if chr(97+i) in S_cut[:-1]:
					continue
				else:
					print(S_cut[:-1]+chr(97+i))
					continuate = False
					break

Solution (Python):
s=input()
if len(s)!=26:
    c = sorted(set("abcdefghijklmnopqrstuvwxyz")-set(s))[0]
    print(s+c)
elif s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    s=list(s)
    m=[s.pop()]
    while 1:
        q=s.pop()
        if max(m)>q:
            c=min(a for a in m if a>q)
            print("".join(s)+c)
            break
        else:
            m.append(q)

Solution (Python):
from string import ascii_lowercase
h = {ascii_lowercase[i]:i for i in range(26)}
S = input()
c = set(ascii_lowercase) - set(S)
if len(c) > 0:
    print(S + sorted(c)[0])
else:
    for i in range(len(S)-1, -1, -1):
        j = h[S[i]]
        for k in range(j+1, 26):
            if not ascii_lowercase[k] in S[:i]:
                print(S[:i] + ascii_lowercase[k])
                exit()
    print(-1)

Solution (Python):
S = input()

def solve(S):
    if len(S) < 26:
        return S + min(set(chr(ord('a') + i) for i in range(26)) - set(S))
    for i in range(len(S) - 1, 0, -1):
        if S[i - 1] < S[i]:
            return S[:i - 1] + min(s for s in S[i:] if s > S[i - 1])
    return "-1"

print(solve(S))

Solution (Python):
s=input()
abc="abcdefghijklmnopqrstuvwxyz"
if len(s)<26:
  print(s+sorted(list(set(abc)-set(s)))[0])
else:
  x=s[-1]
  Tail=[x]
  count,flag=0,0
  for i in range(2,27):
    if s[-i]<x:
      count=i
      flag=1
      break
    else:
      x=s[-i]
      Tail.append(x)
  t="z"
  if count!=0:
    for tt in Tail:
      if tt>s[26-count]:
        t=min(t,tt)  
  print(-1 if flag==0 else s[:-count]+t)

Solution (Python):
S = input()

def next_tasai(S):
  if S == 'zyxwvutsrqponmlkjihgfedcba':
    return -1
  
  A = 'abcdefghijklmnopqrstuvwxyz'
  Aset = set([a for a in A])
  Sset = set([s for s in S])
  
  if (Aset-Sset):
    l = min(Aset-Sset)
    answer = S+l
    return answer
  else:
    i=-1
    while True:
      if S[i] < S[i-1]:
        i-=1
      else:
        key = min([s for s in S[i-1:] if s>S[i-1]])
        return S[:i-1] + key
      
print(next_tasai(S))

Solution (Python):
check='abcdefghijklmnopqrstuvwxyz'
s=input()
l=len(s)
if l<=25:
  for i in range(25):
    if check[i] not in s:
      print(s+check[i])
      break
else:
  s2=''.join(map(str,sorted(s,reverse=True)))
  if s==s2:
    print(-1)
  else:
    for i in range(25):
      if s[-(i+1)]>s[-(i+2)]:
        tmp1=s[:-(i+2)]
        tmp2=s[-(i+2):]
        break
    base=tmp2[0]
    minc=10**9
    for i in range(1,len(tmp2)):
      tmpv=ord(tmp2[i])-ord(base)
      if tmpv>0 and tmpv<minc:
        minc=tmpv
    print(tmp1+chr(ord(base)+minc))

Solution (Python):
s = input()

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


if len(s)!=26:
    for i in range(26):
        if chr(97+i) not in s:
            print(s+chr(97+i))
            exit()
else:
    for i in range(25):
        if s[24-i]<s[25-i]:
            m = s[25-i]
            for x in s[25-i:]:
                if x > s[24-i]:
                    m = min(m, x)
            print(s[:24-i]+m)
            exit()

Solution (Python):
s=input()+'`';m=-1
for i in range(len(s)):
 t=s[:~i];a=set(map(chr,range(ord(s[~i])+1,123)))-set(t)
 if a:m=t+min(a);break
print(m)

Solution (Python):
s=input()+'`';m=-1
for i in range(len(s)):
 t=s[:~i];a=set(map(chr,range(ord(s[~i])+1,123)))-set(t)
 if a:m=t+min(a);break
print(m)

Solution (Python):
s = input()+"`"
for j in reversed(range(len(s))):
    a=set(map(chr,range(ord(s[j])+1,123)))-set(s[:j])
    if len(a)>0:
        print(s[:j]+min(a))
        exit()
print(-1)

Solution (Python):
s = input()+"`"

for j in reversed(range(len(s))):
    sets=set(s[:j])
    for i in range(ord(s[j])+1,123):
        if chr(i) not in sets:
            print(s[:j] + chr(i))
            exit()
print(-1)

Solution (Python):
import sys

S = input()
alphabets = 'abcdefghijklmnopqrstuvwxyz'
seen = set(list(S))

if len(S) < 26:
    for c in alphabets:
        if c not in seen:
            print(S + c)
            sys.exit()

for i in range(len(S)):
    t = S[-1-i]
    if t != 'z':
        for t_dash in alphabets[alphabets.find(t)+1:]:
            if t_dash not in seen:
                print(S[:-1-i] + t_dash)
                sys.exit()
    seen.remove(t)

print('-1')

Solution (Python):
a = set(["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"])
s = input()
if len(s)<26:
    b = list(a-set(s))
    b.sort()
    print(s+b[0])
else:
    for i in range(2,27):
        if s[-i] < s[-i+1]:
            break
        if i == 26:
            print(-1)
            exit()
    for j in sorted(s[-i+1:]):
        if j > s[-i]:break
    print(s[:-i]+j)

Solution (Python):
import numpy as np
alp = [chr(v) for v in range(ord("a"), ord("z") + 1)]
inp = input()

if inp[::-1] == "".join(alp):
    print(-1)
    exit()

if len(inp) == len(alp):
    prev = inp[0]
    indices = [0]
    for index, char in enumerate(inp[1:]):
        if prev < char:
            indices.append(index)
        prev = char
    
    
    
    
    
    
    
    
    
    
    
    index = indices[-1]
    targets = inp[index + 1:]
    for v in sorted(targets):
        if inp[index] < v:
            break
    print(inp[:index] + v)

else:
    print(inp + min(set(alp) - set(list(inp))))

Solution (Python):



N = input()
p = list("abcdefghijklmnopqrstuvwxyz")

if len(N) != 26:
    for i in N:
        p.pop(p.index(i))
    if p == []:
        print(-1)
        exit()
    print(N+p[0])
else:
    tmp = 0
    a = list("zyxwvutsrqponmlkjihgfedcba")
    if a == list(N):
        print("-1")
        exit()
    for i in range(24,-1,-1):
        for c in p:
            if c not in N[:i] and c > N[i]:
                print(N[:i]+c)
                exit()

Solution (Python):
s = input()

dic = "abcdefghijklmnopqrstuvwxyz"

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

elif len(s) < 26:
    t = ""
    for x in dic:
        if s.count(x) == 0:
            t = x
            break
    print(s+t)
else:
    for idx,x in enumerate(s):
        t = s[-idx-1]
        l = s[-idx-1:]
        min_s = ""
        for y in l:
            if t < y:
                min_s = y if min_s=="" else min(min_s, y)
        
        if min_s != "":
            break
    print(s[:-idx-1]+min_s)

Solution (Python):
import string

s = input()

if len(s) != 26:
  mins = min(set(string.ascii_lowercase) - set(s))
  print(s + mins)
else:
  is_re = True
  for i, (nexts, prevs) in enumerate(zip(s[::-1], s[::-1][1:])):
    if nexts > prevs:
      is_re = False
      break

  if is_re:
    print(-1)
  else:
    minq = None
    for _s in s[26-i-1:]:
      if minq is None:
        minq = _s
      else:
        if _s > s[26-i-2] and minq > _s:
          minq = _s
    print(s[:26-i-2] + minq)

Solution (Python):
S = input()
N = len(S)
l = [0 for i in range(26)]
if N < 26:
    for i in range(N):
        l[ord(S[i])-97] += 1
    for i in range(26):
        if l[i] == 0:
            print(S+str(chr(i+97)))
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    m = 0
    for i in reversed(range(26)):
        if m > ord(S[i])-97:
            id = i
            break
        m = ord(S[i])-97
    for i in range(id,26):
        l[ord(S[i])-97] += 1
    for i in range(len(l)):
        if l[i] == 1 and ord(S[id])-97 < i:
            s = chr(i+97)
            break
    print(S[:id]+str(s))

Solution (Python):
S = input()
A = "abcdefghijklmnopqrstuvwxyz"
if len(S)<26:
    for c in A:
        if c not in S:
            print(S+c)
            break
elif S=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for i in range(24,-1,-1):
        for c in A:
            if c not in S[:i] and c>S[i]:
                print(S[:i]+c)
                break
        else:
            continue
        break

Solution (Python):
import sys
s = input()
L = []
alp = [chr(ord('a') + i) for i in range(26)]
TF = [False]*26
for i in range(len(s)):
  if not s[i] in L:
    L.append(s[i])
    TF[ord(s[i])-97] = True
if len(L) != 26:
  for i in range(26):
    if TF[i] == False:
      k = alp[i]
      break
  print(s+k)
else:
  right = s[-1]
  for i in reversed(range(len(s))):
    k = s[i]
    if k < right:
      for j in range(ord(k),ord(k)+26):
        if chr(j) in s[i+1:]:
          print(s[:i]+chr(j))
          break
      sys.exit()
    else:
      right = max(k,right)
  print(-1)

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

if N < 26:
    for i in range(97, 123):
        if chr(i) not in S:
            print(S + chr(i))
            exit()

min_c = [S[-1]]
for c, i in zip(S[-2::-1], range(24, -1, -1)):
    for mc in min_c:
        if c < mc:
            print(S[:i] + mc)
            exit()
    else:
        min_c.append(c)
        min_c.sort()
print(-1)

Solution (Python):
s = input()
l = list('abcdefghijklmnopqrstuvwxyz')
for s_ in s:
    l.remove(s_)
if l==[]:
    l.append(s[-1])
    for i in range(len(s)-2,-1,-1):
        if s[i]<max(l):
            for k in l:
                if k>s[i]:
                    print(s[:i]+k)
                    exit()
        l.append(s[i])
    print(-1)
else:
    print(s+l[0])

Solution (Python):
import numpy as np
alpha = list('abcdefghijklmnopqrstuvwxyz')
alpha_num = [num for num in range(26)]

d = {char:num for (char, num) in zip(alpha, range(26))}

S = input()
S_num = np.array([d[char] for char in S])
last = S_num[-1]

flag = True

if len(S) < 26:
  S_num.sort()
  for k in range(len(S_num)):
    if S_num[k] != alpha_num[k]:
      print(S + alpha[k])
      flag = False
      break
  if flag:
    print(S + alpha[len(S)])

else:
  for k in reversed(range(1, 26)):
    if S_num[k] > S_num[k-1]:
      A = S_num[k:]
      A = A[A > S_num[k-1]]
      A.sort()
      print(S[:k-1] + alpha[A[0]])
      flag = False
      break
  if flag:
    print(-1)

Solution (Python):
S = input()

alph = [0] * 26

for index, i in enumerate(S):
    pointer = ord(i) - ord("a")
    alph[pointer] += 1


if(len(S) == 26):
    if(S=="zyxwvutsrqponmlkjihgfedcba"):
        print(-1)
        exit()
    else:
        old=""
        for i, s1 in enumerate(S[::-1]):
            if i != 0 and s1 < old[-1]:
                out = S[:len(S)-i-1] + min([o for o in old if o > s1])
                print(out)
                exit()
            old += s1
        
else:
    for i in range(26):
        if(alph[i]==0):
            print(S+chr(i+ord("a")))
            exit()

Solution (Python):
s=input()
last='zyxwvutsrqponmlkjihgfedcba'
if s==last:print(-1);exit()
d={c for c in s}
lest={c for c in last}-d
if len(lest)>0:print(s+sorted(lest)[0]);exit()
m=[s[-1]]
for i in range(25)[::-1]:
  for n in sorted(m):
    if s[i]<n:print(s[:i]+n);exit()
  m+=[s[i]]

Solution (Python):


import sys
import heapq


INF = float('inf')
MOD = 10 ** 9 + 7

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 LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    s = SI()

    ss = [ord(c) for c in s]
    abc = [ord(c) for c in 'abcdefghijklmnopqrstuvwxyz']
    if len(s) < 26:
        sss = set(ss)
        for c in abc:
            if c not in sss:
                return s + chr(c)
    hq = []
    prev = ss[-1]
    for i in range(len(s))[::-1]:
        c = ss[i]
        if c >= prev:
            heapq.heappush(hq, c)
        else:
            while hq and hq[0] < c:
                heapq.heappop(hq)
            return s[:i] + chr(hq[0])
        prev = c

    return -1


print(main())

Solution (Python):
S = input()
A = [chr(i) for i in range(ord("a"),ord("z")+1)]

if len(S) < 26 :
    for i in range(len(A)) :
        if A[i] not in S :
            ans = S + A[i]
            break
elif S == "".join(A[::-1]) :
    ans = -1
else :
    B = []
    for i in range(len(A)-1) :
        right = S[len(A)-1-i]
        left = S[len(A)-1-(i+1)]
        B.append(right)
        if left < right:
            last = "z"
            for b in B :
                if left > b :
                    continue
                last = min(last,b)
            ans = S[:len(S)-1-(i+1)] + last
            break
print(ans)

Solution (Python):
s=input()
b="abcdefghijklmnopqrstuvwxyz"
from collections import defaultdict 
if len(s)<26:
  d=defaultdict(int)
  for c in s:
    d[c]+=1
  for bb in b:
    if d[bb]==0:
      print(s+bb)
      exit() 
N=len(s)
lst=[]
for first in range(N-1,0,-1):
  mn=s[first]
  for i in range(first-1,-1, -1):
    if s[i]<mn:
      lst.append(s[:i]+mn)
      
      
if lst:
  print(sorted(lst)[0])
  exit()
print(-1)

Solution (Python):
s = input()

if len(s) != 26:
    se = set([])
    for w in s:
        se.add(w)

    for i in range(26):
        w = chr(ord('a')+i)
        if w not in se:
            print(s + w)
            exit()

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

se = set([])
for i in reversed(range(26)):
    se.add(s[i])
    for j in range(ord(s[i])-ord('a')+1, 26):
        if chr(ord('a')+j) in se:
            print(s[:i]+chr(ord('a')+j))
            exit()

Solution (Python):
S = input()
if len(S) < 26:
    for i in range(26):
        c = chr(ord('a')+i)
        if c not in S:
            print(S + c)
            exit()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

cs = set()
for i in range(25):
    t,tt = S[-i-1],S[-i-2]
    cs.add(t)
    if tt < t:
        last = 'z'
        for c in cs:
            if c < tt: continue
            last = min(last, c)
        print(S[:-i-2] + last)
        exit()

Solution (Python):
import string

alp = string.ascii_lowercase

def main():
    s = input()
    if len(s) < 26:
        for i in alp:
            if i not in s:
                print (s + i)
                return
    if s == alp[::-1]:
        print(-1)
        return
    L = len(s)
    tmp = []
    tt = ''
    for i, ss in enumerate( s ):
        tmp += [ s[L-i-1] ]
        if s[L-i-1] > s[L-i-2]:
            ans = s[:L-i-2]
            tt = s[L-i-2] 
            break
    tmp.sort()
    for i in tmp:
        if i > tt:
            print (ans + i)
            break
main()

Solution (Python):
S = input()

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

tmp = {a: 0 for a in 'abcdefghijklmnopqrstuvwxyz'}

for x in S:
    tmp[x] += 1

tmp2 = []
if all(x == 1 for x in tmp.values()):
    for i in range(len(S)-1, 0, -1):
        tmp2.append(S[i])
        if S[i-1] < S[i]:
            tmp2 = [x for x in tmp2 if x > S[i-1]]
            print(S[:i-1]+min(tmp2))
            exit()
else:
    for k, v in tmp.items():
        if v == 0:
            tmp2.append(k)
    print(S+min(tmp2))

Solution (Python):
import string

alphabet = string.ascii_lowercase

S = input()
count = list(map(lambda x: S.count(x), alphabet))


for a in alphabet:
    if count[alphabet.index(a)] == 0:
        print(S + a)
        exit()

count = [0 for _ in range(len(alphabet))]
cand = ""


for i, s in enumerate(S[::-1]):
    cand += s
    if i == len(S) - 1:
        print(-1)
        exit()
    res = ""
    for a in cand:
        if ord(S[-(i + 2)]) < ord(a):
            if res and res < S[: len(S) - i - 2] + a:
                exit
            else:
                res = S[: len(S) - i - 2] + a
    if res:
        print(res)
        exit()

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

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

atoz = list(map(chr,range(97,123)))
charmap = dict()
for c in atoz:
    charmap[c] = 0

for s in S:
    charmap[s] += 1

flag = 0
for c in atoz:
    if charmap[c] == 0:
        flag = 1
        S = S + c
        break

if flag == 0:
    index = 0
    for i in range(len(S)-1):
        if S[-1-i] > S[-2-i]:
            index = -2-i
            break
    tmp = sorted(list(S[index+1:]))
    for t in tmp:
        if t > S[index]:
            S = S[:index]+t
            break

print(S)

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

A=["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"]
N=[0 for i in range(26)]
n=[i for i in range(26)]
a=[]

if S=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(S)<26:
    for i in range(len(S)):
        for j in range(26):
            if S[i]==A[j]:
                N[j]=1
    for i in range(26):
        if N[i]==0:
            S=S+A[i]
            print(S)
            exit()
else:
    S=list(S)
    for i in range(len(S)):
        for j in range(26):
            if S[i]==A[j]:
                S[i]=n[j]
    for i in range(25):
        if S[25-i]>S[24-i]:
            break
    L=S[25-i:]
    L.sort()
    X=S[:24-i]
    Y=L[bisect.bisect_left(L,S[24-i])]
    X.append(Y)
   
    for i in range(len(X)):
        for j in range(26):
            if X[i]==n[j]:
                X[i]=A[j]
    print("".join(X))

Solution (Python):
alphabet = "abcdefghijklmnopqrstuvwxyz"
S = raw_input()
if len(S) < 26:
  for ab in alphabet:
    if not ab in S:
      print S+ab
      break
else:
  i = -1
  s = "Z"
  while i > -27 and s < S[i]:
    s = S[i]
    i -= 1
  if i == -27:
    print "-1"
  else:
    for ab in alphabet[alphabet.index(S[i])+1:]:
      if not ab in S[:i]:
        print S[:i]+ab
        break

Solution (Python):
import collections
import itertools
import numpy as np
import sys
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()

abc = "abcdefghijklmnopqrstuvwxyz"
num = [i for i in range(26)]
dic = {"null":-1}
for i in range(26):
    dic[abc[i]] = num[i]+1

l = []
tmp = "null"
if len(s) == 26:
    for i in range(25,-1,-1):
        if dic[tmp] < dic[s[i]]:
            tmp = s[i]
            l.append(s[i])
        else:
            for j in l:
                if dic[j] < dic[s[i]]:
                    continue
                else:
                    print(s[:i]+j)
                    exit()
                    break
    else:
        print(-1)
else:
    for i in range(26):
        if abc[i] in s:
            continue
        else:
            print(s+abc[i])
            break

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

D = list("abcdefghijklmnopqrstuvwxyz")

if len(S) == 26:
  cnt = 0
  L = []
  for i in range(25, -1, -1):
    Flag = True
    for j in range(cnt, 26):
      if S[i] == D[j]:
        L.append(S[i])
        cnt = j
        Flag = False
        break
    if Flag:
      L.sort()
      for k in L:
        if k > S[i]:
          S[i] = k
          break
      print("".join(S[:i + 1]))
      exit()
  print("-1")      
else:
  for i in S:
    D.remove(i)
  print("".join(S) + D[0])

Solution (Python):
import string
A=string.ascii_lowercase
S=input()
if S==A[::-1]:
    print(-1)
elif len(S)==26:
    i=-1
    while S[i-1]>S[i]:
        i-=1
    for j in range(-1,i-1,-1):
        if S[j]>S[i-1]:
            c=S[j]
            break
    print(S[:i-1]+c)
else:
    c=sorted(set(A)-set(S))[0]
    print(S+c)