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 collections import deque
from itertools import dropwhile

alphabet = set('abcdefghijklmnopqrstuvwxyz')

if __name__ == '__main__':
    s = input()
    l = [c for c in alphabet - set(s)]
    if l != []:
        print(s + min(l))
        quit(0)

    deq = deque()
    deq.append(s[-1])
    for i in range(24, -1, -1):
        if s[i] < s[i + 1]:
            small = next(dropwhile(lambda c: c <= s[i], deq))
            print(s[:i] + small)
            quit(0)
        else:
            deq.append(s[i])

    print(-1)

Solution (Python):
S = input()
S = list(S)
li = ['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 s in S:
    if s in li:
        li.remove(s)

if len(li) == 0:
    li = ['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']
    max = 26
    m = []
    for i in range(2,len(S)+1):
        m.append(li.index(S[-i+1]))
        if li.index(S[-i]) < li.index(S[-i+1]):
            m = [x for x in m if x > li.index(S[-i])]
            S[-i] = li[min(m)]
            S = S[:-i+1]
            for s in S:
                print(s,end='')
            exit()
    print(-1)
else:
    S += li[0]
    for s in S:
        print(s,end='')

Solution (Python):
import bisect
ss = input()
ss =list(map(ord, ss))
dic = [i for i in range(97,123)]

for s in ss:
    dic.remove(s)

if len(ss)<26:
    ss.append(min(dic))
else:
    for s in ss[::-1]:
        if len(dic)>0 and s < max(dic):
            ss[ss.index(s)] = dic[bisect.bisect_right(dic,s)]
            break
        dic.append(ss.pop(ss.index(s)))
        dic = sorted(dic)
    else:
        ss = -1
print(ss if ss == -1 else ''.join(list(map(chr,ss))))

Solution (Python):
S = input()
 
chars = "abcdefghijklmnopqrstuvwxyz"
char_set = set(chars)
 
if len(S) == 26:
    for i in range(len(S)-1, -1, -1):
        x = chars.index(S[i])
        y = sorted(set(chars[x+1:])-set(S[:i]))
        
        if len(y):
            print(S[:i]+y[0])
            exit()
else:
    t = sorted(set(chars)-set(S))[0]
    print(S+t)
    exit()
print(-1)

Solution (Python):
N = list(input())
setN = set(N)
D = list('abcdefghijklmnopqrstuvwxyz')
if len(N) < 26:
    for d in D:
        if not d in setN:
            N.append(d)
            print(''.join(N))
            exit()
else:
    N.reverse()
    for i in range(25):
        if N[i] < N[i+1]:
            continue
        else:
            tmp = N[i+1]
            ans = N[i+2:]
            ans.reverse()
            for n in N[:i+2]:
                if ord(n) - ord(tmp) > 0:
                    ans.append(n)
                    print(''.join(ans))
                    exit()
print(-1)

Solution (Python):
N = list(input())
setN = set(N)
D = list('abcdefghijklmnopqrstuvwxyz')
rD = list('zyxwvutsrqponmlkjihgfedcba')
if len(N) < 26:
    for d in D:
        if not d in setN:
            N.append(d)
            print(''.join(N))
            exit()
else:
    N.reverse()
    for i in range(25):
        if N[i] < N[i+1]:
            continue
        else:
            tmp = N[i+1]
            ans = N[i+2:]
            ans.reverse()
            cand = N[:i+2]
            cand.sort()
            for n in N[:i+2]:
                if ord(n) - ord(tmp) > 0:
                    ans.append(n)
                    print(''.join(ans))
                    exit()
print(-1)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def is_tasai(S):
    appare_set = set([])
    for s in S:
        if s in appare_set:
            return False
        appare_set.add(s)
    return True

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    S = input_raw[0]
    appare_set = set([])
    z_pos = 0
    for i in range(len(S)):
        if S[i] == "z":
            z_pos = i
        appare_set.add(S[i])

    not_appare = -1
    for s in range(ord("a"), ord("z") + 1):
        if chr(s) not in appare_set:
            print S + chr(s)
            return
    for i in range(len(S) - 1, -1, -1):
        new_s = chr(ord(S[i]) + 1)
        while new_s <= "z":
            S = S[:i]
            S += new_s
            if is_tasai(S) == True:
                print S
                return
            new_s = chr(ord(new_s) + 1)
        S = S[:i]
    print -1



if __name__ == '__main__':
    main()

Solution (Python):
import string
chars = string.ascii_lowercase
def solve(S):
    if S == chars[::-1]:
        return -1
    for c in chars:
        if c in S:
            continue
        else:
            return S + c
    S = S[::-1]
    for i, c in enumerate(S):
        if i == len(S) - 1:
            continue
        c = S[i + 1]
        ans = S[i + 2:]
        if c == chars[-1]:
            continue
        n = chars[chars.find(c) + 1]
        while n in ans:
            if n == chars[-1]:
                n = -1
                break
            n = chars[chars.find(n)+1]
        if n == -1:
            continue

        ans = ans[::-1]
        ans += n
        return ans
    return -1

if __name__ == "__main__":
    print(solve(input()))

Solution (Python):
S = input()

chars = "abcdefghijklmnopqrstuvwxyz"
char_set = set(chars)

if len(S) == 26:
    for i in range(len(S)-1, -1, -1):
        x = chars.index(S[i])
        y = sorted(set(chars[x+1:])-set(S[:i]))
        
        if len(y):
            print(S[:i]+y[0])
            exit()
else:
    t = sorted(set(chars)-set(S))[0]
    print(S+t)
    exit()
print(-1)

Solution (Python):
s = input()

alp = [0]*26
a_int = ord('a')

ans = -1
cand = []
if len(s) == 26:
    for i in reversed(range(1, 26)):
        if s[i] < s[i-1]:
            cand.append(s[i])
            continue
        else:
            cand.append(s[i])
            cand = sorted(cand, reverse=True)
            c = min(list(filter(lambda x: x > s[i-1], cand)))
            ans = s[0:i-1]+c
            break

else:
    for c in s:
        alp[ord(c)-a_int] = 1

    for i in range(26):
        if alp[i] == 0:
            ans = s+chr(i+a_int)
            break
print(ans)

Solution (Python):


import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, initial=1)
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
N = len(S)

c = Counter(S)
if N < 26:
    for s in ascii_lowercase:
        if s not in c:
            print(S + s)
            exit()
else:
    
    for i in range(N-1, -1, -1):
        pref = S[:i]
        S2 = S[i:]
        
        S3 = ''.join(sorted(S2, reverse=True))
        
        if S2 != S3:
            
            print(pref + S3[S3.index(S2[0])-1])
            exit()
    print(-1)

Solution (Python):
s = input()
alpha = "abcdefghijklmnopqrstuvwxyz"
tmpset = set()
n = len(s)
if n != 26:
    for i in range(n):
        tmpset.add(s[i])
    for i in range(26):
        if alpha[i] not in tmpset:
            s = s + alpha[i]
            print(s)
            exit()
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        for i in range(n - 1, 0, -1):
            if s[i] > s[i - 1]:
                tmpset.add(s[i])
                for j in range(26):
                    if alpha[j] in tmpset:
                        if s[i - 1] < alpha[j]:
                            s = s[:i - 1] + alpha[j]
                            print(s)
                            exit()
            else:
                tmpset.add(s[i])

Solution (Python):
import sys
s = input() + chr(96)

for j in range(len(s) - 1, -1, -1):
    sets = set(list(s[:j]))
    for i in range(ord(s[j]) + 1, ord('z') + 1):
        if chr(i) not in sets:
            print(s[:j] + chr(i))
            sys.exit(0)
print(-1)

Solution (Python):
n=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"]
if n=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    if len(n)==26:
        q=a.index(n[-1])
        c=0
        d=[n[-1]]
        for i in range(24,-1,-1):
            c+=1
            d.append(n[i])
            d.sort()
            if d.index(n[i])<len(d)-1:
                print(n[:26-c-1]+d[d.index(n[i])+1])
                break
    else:
        for i in range(len(n)):
            a.pop(a.index(n[i]))
        print(n+a[0])

Solution (Python):
s=list(input())
al=[chr(ord('a') + i) for i in range(26)]
al=[i for i in al if i not in s]
if al:
    print(''.join(s)+al[0])
    exit()
for i in range(25,0,-1):
    if s[i-1]<s[i]:
        a=[j for j in s[i-1:] if s[i-1]<j]
        s=s[:i-1]+[sorted(a)[0]]
        print(''.join(s))
        break
        exit()
else:print(-1)

Solution (Python):
S = input()

string = 'abcdefghijklmnopqrstuvwxyz'
r_string = 'zyxwvutsrqponmlkjihgfedcba'

string = list(string)

if len(S)<26:
    
    for i in range(len(S)):
        for j in range(len(string)):
            if S[i]==string[j]:
                del string[j]
                break
    
    print(S+string[0])
elif S==r_string:
    print(-1)
else:
    for i in range(24, -1, -1):
        if S[i]<S[i+1]:
            end = i
            break
        
    now = S[end+1]
    th = S[end]
    
    for i in range(end+2, 26):
        if th<S[i]:
            if S[i]<now:
                now = S[i]
                
    print(S[0:end]+now)

Solution (Python):
def num(s):
    return ord(s) - ord("a")

S = input()

used = [False] * 26

for s in S:
    used[num(s)] = True

if len(S) != 26:
    for i in range(26):
        if not used[i]:
            ans = S + chr(ord("a") + i)
            break
    print(ans)
else:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        for i in range(len(S) - 1, -1, -1):
            s = S[i]
            used[num(s)] = False
            isOk = False
            for j in range(num(s) + 1, 26, 1):
                if not used[j]:
                    isOk = True
                    last = j
                    break
            if isOk:
                ans = S[:i] + chr(ord("a") + j)
                print(ans)
                break

Solution (Python):
def swapeds(s,a,b):
    return s.translate(str.maketrans({a:b,b:a}))
s = input()
arr = [1 for _ in range(26)]
ok = True
for i in s:
    if arr[ord(i)-97] > 0:
        arr[ord(i)-97] = 0
    else:
        ok = False
        break
mina = 25
for i in range(26):
    if arr[i] > 0 :
        mina = i
        break
        
if ok and s != 'zyxwvutsrqponmlkjihgfedcba':
    if len(s) < 26:
        print(s+chr(mina+97))
    else:
        for i in range(1,26)[::-1]:
            if s[i-1] < s[i]:
                tmp = s[i-1]
                maxs = 'z'
                break
        for i in range(s.index(tmp)+1,26)[::-1]:
            if tmp  < s[i] and maxs > s[i]:
                maxs = s[i]
        print(s[0:s.index(tmp)]+maxs)
else:
    print(-1)

Solution (Python):



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


def inpl(): return list(map(int, input().split()))


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

abc = 'abcdefghijklmnopqrstuvwxyz'
used = set()

ans = ''
if len(s) != 26:
    for c in abc:
        if c not in s:
            print(s + c)
            exit()

else:
    prev_c = ord(s[-1])
    candidate = []
    for i in range(25)[::-1]:
        curr = ord(s[i])
        next = ord(s[i + 1])
        if curr >= next:
            candidate.append(next)
        else:
            candidate.append(next)
            ct = ord('z')
            for c_int in candidate:
                if c_int < curr: continue
                ct = min(ct, c_int)
            print(s[:i] + chr(ct))
            exit()

Solution (Python):
S = input()

ans = ''

if len(S) != 26:
    a_z = [chr(i) for i in range(97, 123)]
    for s in S:
        a_z.remove(s)
    ans = S + a_z[0]
    print(ans)
else:
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        ans = -1
        print(ans)
    else:
        a_z = [int(i) for i in range(97, 123)]
        inputs = list(map(ord, list(S)))
        buf = [inputs[-1]]
        for i in range(1, len(S)):
            if inputs[25-i] > inputs[25-(i-1)]:
                buf.append(inputs[25-i])
            else:
                buf.sort()
                for item in buf:
                    if inputs[25-i] < item:
                        last = item
                        break
                print(''.join(list(map(chr, inputs[:25-i]+[last]))))
                break

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

chara=list("abcdefghijklmnopqrstuvwxyz")
chara_dic={}
for i in range(len(chara)):
	chara_dic[chara[i]]=i
chara_dic_2={}
for i in range(len(chara)):
	chara_dic_2[i]=chara[i]

count={}
for s in S:
	count[s]=count.get(s,0)+1

flag=0
if len(count)==len(chara):
	cur_chara=len(S)-1
	for pt in range(len(S)-2,-1,-1):
		if chara_dic[S[cur_chara]]<chara_dic[S[pt]]:
			cur_chara-=1
		else:
			flag=1
			ans=''
			for i in range(0,pt):
				ans+=str(S[i])
			ls=[]
			for i in range(pt+1,len(S)):
				if chara_dic[S[pt]]<chara_dic[S[i]]:
						ls.append(chara_dic[S[i]])
			ans+=chara_dic_2[min(ls)]
			break
else:
	flag=1
	ans=''
	for i in range(len(S)):
		ans+=S[i]
	for i in range(len(chara)):
		if chara[i] not in S:
			end=chara[i]
			break
	ans+=end

print(ans if flag else "-1")

Solution (Python):
S = input()
chars = list(map(lambda c: ord(c)-97, S))
if len(chars)!=26:
    chars = sorted(chars)
    i = 0
    while i==chars[i]:
        i += 1
        if i==len(chars):
            break
    ans = S + chr(i+97)
else:
    prev = -1
    i = 0
    for c in reversed(chars):
        if prev < c:
            i += 1
            prev = c
        else:
            break
    if i==26:
        ans = -1
    else:
        max_s = chars[-i-1]
        min_c = 27
        for c in chars[-i:]:
            if c>max_s and c<min_c:
                min_c = c
        ans = S[:-i-1] + chr(min_c+97)

print(ans)

Solution (Python):
a={chr(i) for i in range(ord("a"),ord("a")+26)}
s=input()
n=len(s)
if n<26:
  print(s+sorted(a-set(s))[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):
S=list(input())
N=len(S)
alphabet="abcdefghijklmnopqrstuvwxyz"
Appeared,A_to_N={},{}
for i in range(26):
  Appeared[alphabet[i]]=False
  A_to_N[alphabet[i]]=i
if N<26:
  for i in range(N):
    Appeared[S[i]]=True
  for i in range(26):
    if not Appeared[alphabet[i]]:
      S.append(alphabet[i])
      print("".join(S))
      exit()

last=A_to_N[S[-1]]
a=[S[-1]]
for i in range(N-1)[::-1]:
  if A_to_N[S[i]]>last:
    a.append(S[i])
    last=A_to_N[S[i]]
  else:
    for k in a:
      if A_to_N[S[i]]<A_to_N[k]:
        S[i]=k
        print("".join(S[:i+1]))
        exit()
print(-1)

Solution (Python):
S = input()
chr_list = [1] * 26

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for s in S:
        chr_list[ord(s) - ord("a")] = 0

    if all(map(lambda c: c == 0, chr_list)):
        c_set = [S[-1]]
        for i in range(24, -1, -1):
            if not ord(S[i]) > ord(S[i+1]):
                c_set = [c for c in c_set if c >= S[i]]
                print(S[:i] + min(c_set))
                break
            c_set.append(S[i])
    else:
        for i, v in enumerate(chr_list):
            if v == 1:
                print(S + chr(ord("a") + i))
                break

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

s = []
for i in range(24,-1,-1):
    if S[i] > S[i+1]:
        s.append(S[i+1])
        continue
    else:
        s.append(S[i+1])
        last = 'z'
        for c in s:
            if c < S[i]: continue
            last = min(last, c)
        print(S[:i] + last)
        exit()

Solution (Python):
S = input()
l = len(S)
if l != 26:
    colors = [0]*26
    for i in range(l):
        colors[ ord(S[i]) - ord('a')] = 1
    for i in range(26):
        if colors[i] == 0:
            ans = S + chr( i + ord('a'))
            break
    print(ans)
elif S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for i in range(25,0,-1):
        if S[i] > S[i-1]:
            now = i
            break
    colors = [0]*26
    for i in range(now-1):
        colors[ ord(S[i]) - ord('a')] = 1
    c = 'z'
    for i in range(now-1,26):
        if S[now-1] < S[i]:
            c = min(c, S[i])
    print(S[:now-1]+c)

Solution (Python):
S=input()
flag = False
if S=="zyxwvutsrqponmlkjihgfedcba":
    T="-1"
elif len(S)<=25:
    for i in range(97,123):
        if chr(i) not in S:
            T=S+chr(i)
            break
else:
    for i in range(2,27):
        for j in range(ord(S[-i])+1,123):
            if chr(j) not in S[:-i]:
                T=S[:-i]+chr(j)
                flag=True
                break
        if flag:
            break
print(T)

Solution (Python):
S = input()
N = len(S)
abc = list('abcdefghijklmnopqrstuvwxyz')
if N < 26:
    for c in abc:
        if c not in S:
            print(S + c)
            exit()


def dfs(s):
    if s > S:
        print(s)
        exit()
    elif s == S[:len(s)]:
        for c in abc:
            if c not in s:
                dfs(s + c)


dfs('')
print(-1)

Solution (Python):
s=input()
l=[0]*26
for i in s:
    l[ord(i)-ord("a")]=1
if len(s)<26:
    for i in range(26):
        if l[i]==0:
            print(s+chr(i+ord("a")))
            exit()
else:
    for i in range(26):
        l[ord(s[-i-1])-ord("a")]=0
        for j in range(26):
            if l[j]==0 and ord(s[-i-1])-ord("a")<j:
                print(s[:-i-1]+chr(j+ord("a")))
                exit()
print(-1)

Solution (Python):
import sys


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


def next_permutation(l: list) -> bool:
    n = len(l)
    i = n - 2
    while i >= 0 and l[i] >= l[i + 1]:
        i -= 1

    if i == -1:
        return False

    j = i + 1
    while j < n and l[j] > l[i]:
        j += 1
    j -= 1

    l[i], l[j] = l[j], l[i]

    left = i + 1
    right = n - 1

    while left < right:
        l[left], l[right] = l[right], l[left]
        left += 1
        right -= 1

    return True


def main() -> None:
    s = next_str()
    t = [a for a in s]

    for v in range(ord('a'), ord('z') + 1):
        if chr(v) not in s:
            print(s + chr(v))
            return

    if not next_permutation(t):
        print(-1)
    else:
        i = 0
        while s[i] == t[i]:
            print(s[i], end="")
            i += 1
        print(t[i])


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
alpo = 'abcdefghijklmnopqrstuvwxyz'
alp = [False for _ in range(26)]
if len(S) < 26:
    for i in S:
        alp[ord(i)-97] = True
    for k,j in enumerate(alp):
        if not j:
            ans = S + chr(k+97)
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    candi = [0]
    for i in range(26):
        if ord(S[-1-i]) < max(candi):
            for j in sorted(candi):
                if j > ord(S[-1-i]):
                    ans = S[:25-i]+chr(j)
                    break
            break
        candi.append(ord(S[-1-i]))
print(ans)

Solution (Python):
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
        print("-1")
        exit()
S = list(S)
S = [ord(e) for e in S]
ord_list = list(range(97,123))

for e in ord_list:
        if not e in S:
                S.append(e)
                S = [chr(e) for e in S]
                print("".join(S))
                exit()

for i in range(len(S)-2,-1,-1):
        note = 123
        for j in range(i+1,len(S)):
                if S[i] < S[j] and S[j] < note:
                        note = S[j]
        if note < 123:
                S[i] = note
                del S[i+1:]
                S = [chr(e) for e in S]
                print("".join(S))
                break

Solution (Python):
S = input()

abc = 'abcdefghijklmnopqrstuvwxyz'

less_abc = abc

for s in S:
    less_abc = less_abc.replace(s,'')

ans = ''
if len(S) != 26:
    ans = S + less_abc[0]
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    bef_tmp = -1
    tmp = ''
    for s in S[::-1]:
        ind = abc.index(s)
        if ind > bef_tmp:
            bef_tmp = ind
            tmp += s
        else:
            ans = S[:S.index(s)]

            tar_s = s
            ind = abc.index(tar_s)
            for ss in abc[ind:]:
                if ss in tmp:
                    break

            ans += ss
            break

    
    
    
    
    
    
    
    
    
    
    
    

    
    

    
    

    
    
    
    
    
    

print(ans)

Solution (Python):

s = input()
x = list("abcdefghijklmnopqrstuvwxyz")

if len(s)<26:
    for c in x:
        if c not in s:
            print(s+c)
            exit()
else:
    s = list(s)
    d = set()
    c = s[-1]
    d.add(c)
    i = len(s)-2
    while True:
        if i<0:
            break
        elif s[i]>c:
            c = s[i]
            i -= 1
            d.add(c)
        else:
            break
    if i>=0:
        minc = None
        for c in d:
            if c<s[i]:
                continue
            elif minc is None:
                minc = c
            elif c<minc:
                minc = c
        if minc is not None:
            s[i] = minc
            s = s[:i+1]
            print("".join(s))
            exit()
print(-1)

Solution (Python):

from sys import stdin

s_in = lambda: stdin.readline()[:-1]  


ALPHABET = [i for i in range(ord('a'), ord('z') + 1)]
S = s_in()

if len(S) < len(ALPHABET):
    list_s = list(S)
    for a in ALPHABET:
        if chr(a) not in list_s:
            S += chr(a)
            break
    print(S)
else:
    reversed_s = list(S)[::-1]
    last_s = reversed_s[0]
    cand = [ord(last_s)]
    ans = ''
    for i in range(len(reversed_s[1:])):
        s = reversed_s[i + 1]
        if ord(s) > ord(last_s):
            last_s = s
            cand.append(ord(last_s))
        else:
            min_s = float('inf')
            for c in cand:
                if c > ord(s):
                    min_s = min(min_s, c)
            if (i + 2) == len(reversed_s):
                ans = chr(min_s)
            else:
                ans += ''.join(reversed_s[i + 2:][::-1]) + chr(min_s)
            break
    if ans == '':
        print(-1)
    else:
        print(ans)

Solution (Python):
S = input()
AB = ["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:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        out, Fin = [], False
        for i in range(26):
            out.append(AB.index(S[-1]))
            out.sort()
            S = S[:len(S)-1]
            for o in out:
                if AB.index(S[-1]) < o:
                    S = S[:len(S)-1] + AB[o]
                    print(S)
                    Fin = True
                    break
            if Fin:
                break
else:
    for i in AB:
        if i not in set(S):
            S += i
            print(S)
            break

Solution (Python):
S = input()
if len(S) < 26:
    for i in range(26):
        if chr(ord('a')+i) not in S:
            print(S + chr(ord('a')+i))
            exit()
for i in range(len(S)-1, -1, -1):
    minC = ord('z')+1
    for j in range(len(S)-1, i, -1):
        if ord(S[j]) > ord(S[i]):
            minC = min(minC, ord(S[j]))
    if minC < ord('z')+1:
        print(S[:i] + chr(minC))
        exit()
print(-1)

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

usable = [not a in S for a in "abcdefghijklmnopqrstuvwxyz"]

for i in range(len(usable)):
    if usable[i]:
        print(S + chr(ord('a') +i))
        exit()
else:
    while True:
        poped_char = S[-1]
        usable[ord(poped_char) - ord('a')] = True
        S = S[:-1]
        
        for i in range(ord(poped_char)-ord('a')+1,len(usable)):
            if usable[i]:
                print(S + chr(ord('a') +i))
                exit()

Solution (Python):
import sys

def forward(s1, c):
    for s in s1:
        if s > c:
            return False
    return True

def get_char(s, c):
    return sorted(list(filter(lambda x: x > c, s)))[0]
    
S = sys.stdin.readline().strip()

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

s_list = [s for s in S]
all_char = "abcdefghijklmnopqrstuvwxyz"

if S == "zyxwvutsrqponmlkjihgfedcba":
    print("-1")
elif len(S) != 26:
    for c in c_list:
        if c not in s_list:
            print(S + c)
            break
else:
    for i in reversed(list(range(25))):
        sub = S[i+1:]
        if forward(sub, S[i]):
            
            pass
        else:
            
            m = get_char(sub, S[i])
            print(S[:i] + m)
            break

Solution (Python):
n = raw_input()

aset = set(n)

alphabets = set("abcdefghijklmnopqrstuvwxyz")

while True:
    if len(n) == 0:
        print -1
        break

    c = n[-1]
    if c == 'z':
        n = n[:-1]
        continue

    if len(aset) != 26:
        print n + min(list(alphabets - aset))
        break

    _next = chr(ord(c)+1)
    n = n[:-1]+_next
    if len(list(set(n))) == len(n):
        print n
        break

Solution (Python):
S = input()

if len(S) == 26:
    tmp = S[:-1]
    f = True
    removed = [S[-1]]
    while len(tmp) > 0:
        removed.sort()
        for r in removed:
            if tmp[-1] < r:
                print(tmp[:-1] + r)
                exit()
        removed.append(tmp[-1])
        tmp = tmp[:-1]


alphabet = ["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"]
words = set(alphabet)
input_words = set()

for s in S:
    input_words.add(s)

margin_word = words - input_words
margin_word = list(margin_word)
margin_word.sort()

if len(margin_word) == 0:
    print(-1)
else:
    print(S + margin_word[0])

Solution (Python):
s=input()
l=[chr(i) for i in range(97,97+26)]
if len(s)!=26:
  for i in l:
    if i not in s:
      print(s+i)
      break
else:
  for j in range(26):
    S=s[j:26]
    c=0
    for k in range(len(S)-1):
      if S[k]>S[k+1]:c+=1
    if c==len(S)-1:
      break
  if j==0:print(-1)
  else:
    for m in range(l.index(s[j-1])+1,26):
      if l[m] not in s[0:j-1]:
        x=l[m]
        break
    print(s[0:j-1]+x)

Solution (Python):
s=input()
l=[chr(i) for i in range(97,97+26)]
if len(s)!=26:
  for i in l:
    if i not in s:
      print(s+i)
      break
else:
  for j in range(26):
    S=s[j:26]
    c=0
    for k in range(len(S)-1):
      if S[k]>S[k+1]:c+=1
    if c==len(S)-1:
      x=j
      break
  if x==0:print(-1)
  else:
    for m in range(l.index(s[x-1])+1,26):
      if l[m] not in s[0:x-1]:
        y=l[m]
        break
    print(s[0:x-1]+y)

Solution (Python):
s = input()
for i in range(97, 97+26):
    if chr(i) not in s:
        print(s + chr(i))
        exit()
a = set()
for i in range(24, -1, -1):
    if s[i] > s[i+1]:
        continue
    else:
        for j in range(ord(s[i])-97+1,26):
            a.add(chr(j+97))
        temp = min(a & set(s[i+1:]))
        print(s[:i] + temp)
        exit()
print(-1)

Solution (Python):
s=input()
x=[0 for _ in range(26)]
if len(s)<26:
    for i in range(len(s)):
        x[ord(s[i])-ord('a')]=1
    for j in range(26):
        if x[j]==0:
            k=j
            break
    print("".join([s,chr(ord('a')+k)]))
elif s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for i in range(2,27):
        if s[-i]<s[-i+1]:
            break
    for j in sorted(s[-i+1:]):
        if j>s[-i]:
            break
    print(s[:-i]+j)