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):



def alphabet_to_int(c):
    if c=="a":
        return 0
    elif c=="b":
        return 1
    elif c=="c":
        return 2
    elif c=="d":
        return 3
    elif c=="e":
        return 4
    elif c=="f":
        return 5
    elif c=="g":
        return 6
    elif c=="h":
        return 7
    elif c=="i":
        return 8
    elif c=="j":
        return 9
    elif c=="k":
        return 10
    elif c=="l":
        return 11
    elif c=="m":
        return 12        
    elif c=="n":
        return 13
    elif c=="o":
        return 14
    elif c=="p":
        return 15
    elif c=="q":
        return 16
    elif c=="r":
        return 17
    elif c=="s":
        return 18
    elif c=="t":
        return 19
    elif c=="u":
        return 20
    elif c=="v":
        return 21
    elif c=="w":
        return 22
    elif c=="x":
        return 23
    elif c=="y":
        return 24              
    elif c=="z":
        return 25        
        
def int_to_alphabet(i):
    if i==0:
        return "a"
    elif i==1:
        return "b"
    elif i==2:
        return "c"
    elif i==3:
        return "d"
    elif i==4:
        return "e"
    elif i==5:
        return "f"
    elif i==6:
        return "g"
    elif i==7:
        return "h"
    elif i==8:
        return "i"
    elif i==9:
        return "j"
    elif i==10:
        return "k"
    elif i==11:
        return "l"
    elif i==12:
        return "m"        
    elif i==13:
        return "n"
    elif i==14:
        return "o"
    elif i==15:
        return "p"
    elif i==16:
        return "q"
    elif i==17:
        return "r"
    elif i==18:
        return "s"
    elif i==19:
        return "t"
    elif i==20:
        return "u"
    elif i==21:
        return "v"
    elif i==22:
        return "w"
    elif i==23:
        return "x"
    elif i==24:
        return "y"              
    elif i==25:
        return "z"        
            
        
S=raw_input()

if len(S)<=25:
    char_set = [0 for i in range(26)]
    for i in range(len(S)):
        char_set[alphabet_to_int(S[i])]=1
    for i in range(26):
        if char_set[i]==0:
            ans=S+int_to_alphabet(i)
            print ans
            break
else:
    prev_char=S[-1]
    prev_set = [0 for i in range(26)]
    prev_set[alphabet_to_int(prev_char)]=1
    for i in range(26):
        if alphabet_to_int(prev_char)>alphabet_to_int(S[-(i+1)]):
            for j in range(alphabet_to_int(S[-(i+1)]),26):
                if prev_set[j] == 1:
                        ans=S[:-(i+1)]+int_to_alphabet(j)
                        break
            print ans
            break
        if i==25:
            print -1
        prev_set[alphabet_to_int(S[-(i+1)])]=1
        prev_char=S[-(i+1)]

Solution (Python):
s=raw_input()
for i in range(ord('a'), ord('a')+26):
	if s.find(chr(i))==-1:
		print s+chr(i)
		exit(0)
for i in range(25, 0, -1):
	if s[i]>s[i-1]:
		print s[0:i-1]+min([ch for ch in s[i:] if ch>s[i-1]])
		quit()
assert s=='zyxwvutsrqponmlkjihgfedcba'
print -1

Solution (Python):
s = str(input())
list_s = list(s)
alp_list = list("abcdefghijklmnopqrstuvwxyz")
dict_alp = {}
ans = 0
for i in range(26):
    dict_alp[alp_list[i]] = i+1
if len(list_s) == 26:
    i = 25
    while True:
        try:
            if dict_alp[list_s[i]] > dict_alp[list_s[i-1]]:
                list_s = list_s[:i]
                x = list_s.pop()
                break
            else:
                i -= 1
        except IndexError:
            ans = -1
            break
    if ans != -1:
        alp_set = []
        for alp in alp_list:
            if not alp in list_s:
                if dict_alp[alp] > dict_alp[x]:
                    alp_set.append(alp)
        alp_set.sort()
        list_s.append(alp_set[0])
else:
    for alp in alp_list:
        if not alp in list_s:
            list_s.append(alp)
            break
if ans == -1:
    print(ans)
else:
    print("".join(list_s))

Solution (Python):
import sys

def isGreater(x, y):
    
    if x.find(y) == 0:
        return True
    for xi, yi in zip(x, y):
        if xi == yi:  continue
        else: 
            if xi > yi: return True
    return False


S = input()

alpha = set([chr(ord("a") + i) for i in range(26)])
S_1 = "".join([chr(ord("a") + i) for i in reversed(range(26))]) 

if len(set(S)) < 26:
    al = sorted(list((alpha - set(S))))
    print(S + al[0])
elif S == S_1:
    print(-1)
else:
    for i in reversed(range(len(S) -1 )):
        for a in sorted(list(alpha - set(S[:i]))):
            S2 = S[:i] + a
            if isGreater(S2, S):
                print(S2)
                sys.exit()

Solution (Python):
a={chr(i) for i in range(97,123)}
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):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
atoz = [chr(i) for i in range(97,97+26)]

S = input()

if len(S) < 26:
    for a in atoz:
        if not a in S:
            print(S+a)
            break
else:
    right = S[-1]
    for i in reversed(range(len(S))):
        s = S[i]
        if s < right:
            for j in range(ord(s),ord(s)+26):
                if chr(j) in S[i+1:]:
                    print(S[:i]+chr(j))
                    break
            sys.exit()
        else:
            right = max(s,right)
    print(-1)

Solution (Python):

s=list(input())
k = 0
if s.count('a') == 0:
    s += 'a'
elif s.count('b') == 0:
    s += 'b'
elif s.count('c') == 0:
    s += 'c'
elif s.count('d') == 0:
    s += 'd'
elif s.count('e') == 0:
    s += 'e'
elif s.count('f') == 0:
    s += 'f'
elif s.count('g') == 0:
    s += 'g'
elif s.count('h') == 0:
    s += 'h'
elif s.count('i') == 0:
    s += 'i'
elif s.count('j') == 0:
    s += 'j'
elif s.count('k') == 0:
    s += 'k'
elif s.count('l') == 0:
    s += 'l'
elif s.count('m') == 0:
    s += 'm'
elif s.count('n') == 0:
    s += 'n'
elif s.count('o') == 0:
    s += 'o'
elif s.count('p') == 0:
    s += 'p'
elif s.count('q') == 0:
    s += 'q'
elif s.count('r') == 0:
    s += 'r'
elif s.count('s') == 0:
    s += 's'
elif s.count('t') == 0:
    s += 't'
elif s.count('u') == 0:
    s += 'u'
elif s.count('v') == 0:
    s += 'v'
elif s.count('w') == 0:
    s += 'w'
elif s.count('x') == 0:
    s += 'x'
elif s.count('y') == 0:
    s += 'y'
elif s.count('z') == 0:
    s += 'z'
else:
    for i in range(25):
        if s[25-i] > s[25-i-1]:
            r = s[25-i]
            for j in range(i+1):
                if s[25-i+j] > s[25-i-1]:
                    r = s[25-i+j]
            s = s[:25-i-1:] + list(r)
            break
        else:
            k += 1
if k == 25:
    print(-1)
else:
    ass = ''
    for i in range(len(s)):
        ass += s[i]
    print(ass)

Solution (Python):
from string import ascii_lowercase
s = input()
if len(s) != 26:
    nokori = set(ascii_lowercase) - set(s)
    print(s + min(nokori))
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
        if i == 26:
            print(-1)
            exit()
    s_ = sorted(s[-i + 1:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i]+j)

Solution (Python):
s=input()
a=list("abcdefghijklmnopqrstuvwxyz")
if len(s)!=26:nokori=sorted(set(a)-set(s),key=a.index);print(s+nokori[0])
else:
 for i in range(2,27):
  if s[-i]<s[-i+1]:
    break
  if i==26:
   print(-1)
   exit()
 t=sorted(s[-i+1:])
 for j in t:
  if j>s[-i]:break
 print(s[:-i]+j)

Solution (Python):
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
        if i == 26:
            print(-1)
            exit()
    s_ = sorted(s[-i + 1:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i] + j)

Solution (Python):
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
    s_ = sorted(s[-i + 1:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i] + j)

Solution (Python):
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
    s_ = sorted(s[-i:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i] + j)

Solution (Python):
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(1, 27):
        if s[-i] > s[-i - 1]:
            break
    s_ = sorted(s[-i:])
    for j in s_:
        if j > s[-i - 1]:
            break
    print(s[:-i - 1] + j)

Solution (Python):
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
     print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(1, 27):
        if list(s)[-i] > list(s)[-(i + 1)]:
            break
    T = sorted(list(s)[-i:])
    
    for t in T:
        if t > list(s)[-(i + 1)]:
            break
    print("".join(list(s)[:-(i + 1)]) + t)

Solution (Python):
def solv(S):
    c = []
    for i in range(25, 0, -1):
        for j in range(i-1, -1, -1):
            if S[i] > S[j]:
                c.append(S[:j]+S[i])
    print(sorted(c)[0])
    return


alph = "abcdefghijklmnopqrstuvwxyz"
S = input()
for c in S:
    alph = alph.replace(c, "")
if alph != "":
    print(S+alph[0])
else:
    alph = "abcdefghijklmnopqrstuvwxyz"
    if S == alph[::-1]:
        print(-1)
    else:
        solv(S)

Solution (Python):
s = input()

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

def youngest(str):
    cnt = [0]*26
    for c in str:
        cnt[ord(c)-ord('a')] += 1
    return chr(ord('a')+cnt.index(0))

if len(s)<26:
    print(s+youngest(s))
    exit()


suffix = ''
for i,c in enumerate(s[::-1]):
    if len(suffix)==0:
        suffix += c
    else:
        if suffix[i-1]>c:
            break
        else:
            suffix += c

c = 'z'
for i in suffix:
    if i>s[25-len(suffix)]:
        c = min(c,i)

print(s[:25-len(suffix)]+c)

Solution (Python):
s = input()
t = list(s)

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

r = []
for c in t:
    r.append(ord(c))

if len(t) < 26:
    r = sorted(r)

    temp = ord('a')

    for x in r:
        if x == temp:
            temp += 1
        else:
            break

    t.append(chr(temp))

    print(''.join(t))

else:
    temp = r[25]
    for i in range(24, -1, -1):
        if r[i] < temp:
            hoge = r[i]
            fuga = sorted(r[i + 1:])
            for x in fuga:
                if x > hoge:
                    hoge = x
                    break
            t[i] = chr(hoge)
            break
        else:
            temp = max(temp, r[i])
            t[i] = ''

    t[25] = ''
    print(''.join(t))

Solution (Python):
s = input()
a = [chr(i) for i in range(ord('a'), ord('z')+1)]
if len(s)< 26:
    for i in a:
        if sorted(s).count(i)==0:
            break
    print(s+i)
else:
    l = [ord(i) for i in s]
    for i in range(1,len(s)):
        if l[-i]-l[-i-1]>0:
            k = sorted(s[-i:])
            for j in k:
                if ord(j)>ord(s[-i-1]):
                    break
            print(s[:-i-1]+j)
            break
    else:
        print(-1)

Solution (Python):
def a_diverse_word(S):
    from string import ascii_lowercase
    if len(S) < 26:
        
        ans = S + min(set(list(ascii_lowercase)) - set(S))
    else:  
        idx = 0  
        for k in range(len(S) - 1, 0, -1):
            if S[k] > S[k - 1]:
                idx = k
                break
        if idx == 0:
            ans = '-1'  
        else:
            for c in sorted(S[idx:]):
                if S[idx - 1] < c:
                    ans = S[:idx - 1] + c
                    break
    return ans

S = input().strip()
print(a_diverse_word(S))

Solution (Python):
def a_diverse_word(S):
    from string import ascii_lowercase
    if len(S) < 26:
        
        ans = S + min(set(list(ascii_lowercase)) - set(S))
    else:  
        idx = 0  
        for k in range(len(S) - 1, 0, -1):
            if S[k] > S[k - 1]:
                idx = k
                break
        if idx == 0:
            ans = '-1'  
        else:
            for c in ascii_lowercase:
                if (S[idx - 1] < c) and (c not in S[:idx]):
                    ans = S[:idx - 1] + c
                    break
    return ans

S = input().strip()
print(a_diverse_word(S))

Solution (Python):
s = input()

s_set = set(s)
a_set = set('abcdefghijklmnopqrstuvwxyz')
left_list = sorted(list(a_set - s_set))

ans = -1
if len(left_list) == 0:
    while ans == -1:
        left_list.append(s[-1])
        s = s[:-1]
        if len(s) == 0:
            break
        left_list = sorted(left_list)
        
        for c in left_list:
            
            if c > s[-1]:
                ans = s[:-1] + c
                break
else:
    ans = s + left_list[0]
print(ans)

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()

cset = set()
for i,(a,b) in enumerate(reversed(list(zip(S,S[1:])))):
    cset.add(b)
    if a < b:
        last = 'z'
        for c in cset:
            if c < a: continue
            last = min(last, c)
        print(S[:-i-2] + last)
        exit()
print(-1)

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

cset = set()
for i,(a,b) in enumerate(reversed(list(zip(S,S[1:])))):
    cset.add(b)
    if a < b:
        last = 'z'
        for c in cset:
            if c < a: continue
            last = min(last, c)
        print(S[:-i-2] + last)
        exit()

Solution (Python):
s = input()

g = set()
for i in s:
    g.add(i)

al = [chr(ord('a') + i) for i in range(26)]
a = 'abcdefghijklmnopqrstuvwxyz'

if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for w in al:
        if w in g:
            continue
        s += w
        print(s)
        break
else:
    for i in reversed(range(26)):
        j = a.find(s[i])
        g.remove(a[j])
        while j < 25:
            j += 1
            s = s[:-1] + a[j]
            if not a[j] in g:
                print(s)
                exit()
        s = s[:-1]

Solution (Python):
s = input()

alphaset = set()
for i in range(97, 97+27):
    alphaset.add(chr(i))
    

if len(s) < 26:
    for c in list(s):
        alphaset.remove(c)
        
    alphalist = list(alphaset)
    alphalist.sort()
    
    print(s + alphalist[0])
    
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        
    else:
        c = list(s)
        point = 100
        for i in range(25,0,-1):
            if c[i-1] > c[i]:
                continue
            else:
                old = c[i-1]
                for ci in c[:i]:
                    alphaset.remove(ci)
                    
                alphalist = list(alphaset)
                alphalist.sort()
                
                j = 0
                while old > alphalist[j]:
                    j += 1
                c[i-1] = alphalist[j]
                point = i
                break
        
        print("".join(c[:point]))

Solution (Python):
def f(s, i, d):
  if len(d) == 0:
    return "-1"
  if len(s) == i:
    d.sort()
    return d[0]
  d.remove(s[i])
  r = f(s, i+1, d)
  if r == "-1":
    d.sort()
    for j in d:
      if s[i] < j:
        return j
    d.append(s[i])
    return "-1"
  return "".join([s[i], r])

def main():
  d = list("abcdefghijklmnopqrstuvwxyz")
  t = input()
  r = f(t, 0, d)
  print(r)

main()

Solution (Python):
s = input()

ans = s
ansa = s
a = 'abcdefghijklmnopqrstuvwxyz'
a = [a[i] for i in range(26)]
if len(s)<26:
    for i in a:
        if ans.count(i) == 0:
            ans += i
            break
    print(ans)
else:
    b = 26
    for i in range(1,26):
        if a.index(s[i]) > a.index(s[i-1]):
            b = i
    if b == 26:
        print(-1)
    else:
        s = s[:b]
        for j in a:
            if a.index(j) > a.index(s[b-1]) and j not in s:
                s = s[:b-1]+j
                break
        print(s)

Solution (Python):
s = input()
from string import ascii_lowercase
a = ascii_lowercase
if s == a[::-1]:
  print(-1)
else:
  if len(set(s))<26:
    for ele in a:
      if ele not in s:
        print(s+ele)
        break
  else:
    suf=[s[-1]]
    for i in range(25, 0, -1):
      if s[i]<s[i-1]:
        suf.append(s[i-1])
      else:
        last = i-1
        break
    suf.sort()
    for i in range(len(suf)):
      if s[last]<suf[i]:
        print(s[:last]+suf[i])
        break

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

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

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"]
for i in alphabet:
    if not i in s:
        print(s + i)
        exit()

flag = True
while n > 0 and flag:
    last = alphabet.index(s[-1])
    for i in alphabet[last + 1:]:
        if i not in s[:-1]:
            print(s[:-1] + i)
            flag = False
            break
    else:
        s = s[:-1]
    n = len(s)

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

if S == "zyxwvutsrqponmlkjihgfedcba":
     print(-1)
        
elif len(S) < 26:
    print(S + min(set(alphabet) - set(S)))
    
else:
    for i in range(1, 27):
        if list(S)[-i] > list(S)[-(i + 1)]:
            break
    T = sorted(list(S)[-i:])
    
    for t in T:
        if t > list(S)[-(i + 1)]:
            break
    print("".join(list(S)[:-(i + 1)]) + t)

Solution (Python):
S=input()

if(len(S)!=26):
 for i in range(ord("a"),ord("z")+1):
  CHARA=chr(i)
  if CHARA not in S:
   print(S+CHARA)
   exit()

S=list(S)
T=[]
while(S):
    for c in T:
        if(c > S[-1]):
            print(''.join(S[:-1]) + c)
            exit()
    T.append(S.pop())
print(-1)

Solution (Python):
s = input()
if(len(s) != 26):
    for c in range(ord('a'), ord('z')+1):
        c = chr(c)
        if(not c in s):
            print(s+c)
            exit()
s = list(s)
t = []
while(s):
    for c in t:
        if(c > s[-1]):
            print(''.join(s[:-1]) + c)
            exit()
    t.append(s.pop())
print(-1)

Solution (Python):
def main():
    s = input()

    ch_lst = [0 for i in range(26)]
    for i in s:
        ch_lst[ord(i)-ord('a')] += 1

    for i in range(len(ch_lst)):
        if ch_lst[i] == 0:
            s += chr(i+ord('a'))
            break

    else:
        for i in range(len(s)-1, -1, -1):
            ch_lst[ord(s[i])-ord('a')] = 0

            for j in range(ord(s[i])-ord('a')+1, 26):
                if ch_lst[j] == 0:
                    s = s[:i]
                    s += chr(j+ord('a'))
                    break

            else:
                continue

            break

        else:
            s = -1

    print(s)


if __name__ == '__main__':
    main()

Solution (Python):
s = raw_input()
if len(s) < 26:
    l = [ chr(ord('a') + i) for i in range(26) ]
    for c in s:
        l.remove(c)
    print s + l[0]
else:
    l = []
    p = -1
    for i in range(25, 0,-1):
        l.append(s[i])
        if s[i-1] < s[i]:
            l.append(s[i-1])
            p = i-1
            break
    if p == -1:
        print -1
    else:
        l.sort()
        print s[:p] + l[l.index(s[p]) + 1]

Solution (Python):
S = input()
T = "abcdefghijklmnopqrstuvwxyz"
A = [str(x) for x in T]

if len(S) != 26:
    for l in S:
        A.remove(l)
    print(S+A[0])
    exit()

if S == T[::-1]:
    print(-1)
    exit(0)

U = []
for k in range(1,26):
    if S[-k-1] > S[-k]:
        U.append(S[-k-1])
        U.append(S[-k])
    else:
        U.append(S[-k-1])
        U.append(S[-k])
        t = S[-k-1]
        S = S[:-k-1]
        break

U.sort()
for l in U:
    if t < l:
        print(S+l)
        exit(0)

Solution (Python):
import sys

def intstr(lis):
    a = ''
    for v in lis:
        a += chr(v+97)
    return a

s = list(input())
numS = []
for v in s:
    numS.append(ord(v)-97)

if len(numS)<26:
    for i in range(0,26):
        if numS.count(i)==0:
            numS.append(i)
            ans = intstr(numS)
            print(ans)
            sys.exit()
else:
    while numS:
        cur = numS.pop()
        for i in range(cur+1,26):
            if numS.count(i)==0:
                numS.append(i)
                ans = intstr(numS)
                print(ans)
                sys.exit()
    print(-1)

Solution (Python):
s = input()
old = ''
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    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
    print(-1)
else:
    print(s + min(set(sample)-set(s)))

Solution (Python):
s = input()
old = ''
flag = 0
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    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
    if flag == 0:
        out = -1
else:
    out = s + min(set(sample)-set(s))

print(out)

Solution (Python):
s = input()
old = ''
flag = 0
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    for i, s1 in enumerate(s[::-1]):
        if i != 0 and s1 < old[-1]:
            for o in sorted(old):
                if o > s1:
                    out = s[:len(s)-i-1] + o
                    print(out)
                    exit()
        old += s1
    if flag == 0:
        out = -1
else:
    out = s + min(set(sample)-set(s))

print(out)

Solution (Python):
s = input()
old = ''
flag = 0
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    u = {s[-1]}
    for i in range(len(s) - 1)[::-1]:
        x = [c for c in u if s[i] < c]
        if x:
            out = s[:i] + min(x)
            break
        u.add(s[i])
    else:
        out = -1
    
    
    
    
    
    
    
    
else:
    out = s + min(set(sample)-set(s))

print(out)

Solution (Python):
s = input()
t = set(map(chr, range(ord('a'), ord('z')+1))) - set(s)
if t:
    print(s + min(t))
else:
    u = {s[-1]}
    for i in range(len(s)-1)[::-1]:
        x = [c for c in u if s[i]<c]
        if x:
            print(s[:i] + min(x))
            break
        u.add(s[i])
    else:
        print(-1)

Solution (Python):
S = input()
chars = "abcdefghijklmnopqrstuvwxyz"
if len(S) < 26:
    for c in chars:
        if c not in S:
            Ans = S+c
            break
else:
    if S == chars[::-1]:
        Ans = -1
    else:
        for i in range(25):
            if S[25-i] > S[25-i-1]:
                break
        for j in range(25): 
            if chr(ord(S[25-i-1])+1+j) not in S[0:25-i-1]:
                break
        Ans = S[0:25-i-1]+chr(ord(S[25-i-1])+1+j)
print(Ans)

Solution (Python):
s = input()


a = ord("a")
z = ord("z")

for i in range(a, z + 1):
    if not chr(i) in s:
        ans = s + chr(i)
        print(ans)
        exit()


deleted = []
current = s
for i in reversed(s):
    for usable in deleted:
        if ord(current[-1]) <  usable:
            print(current[:-1] + chr(usable))
            exit()

    deleted.append(ord(i))
    sorted(deleted)
    current = current[:-1]

print("-1")

Solution (Python):
s= input()
V = list(s)
X = [chr(ord('a') + i) for i in range(26)]

if len(s)<26:
  for i in range(26):
    count1 = s.count(X[i])
    if count1 >= 1:
      continue
    else:
      Nasi = X[i]
      break
  ans = s + Nasi
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    ans= -1
  else:
    for i in range(25, 0, -1):
        if V[i] > V[i-1]:
          ans = V[0:i-1]
          if ans == []:
            ans = chr(ord(V[0]) + 1)
          else:
              B = min(V[i:26])
              while B <= V[i-1]:
                  B = chr(ord(B) + 1)
              while B in ans:
                  B = chr(ord(B) + 1)
              ans.append(B)
              ans = ''.join(ans)
              break
        else:
            continue
print(ans)

Solution (Python):
s = s0 = input()
d = {chr(c) for c in range(97, 123)}
for c in s:
    d.remove(c)
if d:
    s += min(d)
else:
    for c in reversed(s):
        s = s[:-1]
        if d and any(c < e for e in d):
            s += min(e for e in d if c < e)
            break
        d.add(c)
print(s if s else -1)
if s:
    assert s0 < s

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

if len(set(S)) < 26:
    for a in A:
        if a not in S:
            print(S+a)
            break
else:
    X = []
    for i in range(25, 0, -1):
        if S[i] > S[i-1]:
            X.append(S[i])
            X = sorted(list(filter(lambda x: x > S[i-1], X)))
            print(S[:i-1]+X[0])
            break
        else:
            X.append(S[i])
    else:
        print(-1)

Solution (Python):
def solve(S):
    chars = [chr(c) for c in range(ord("a"), ord("z") + 1)]
    cs = dict((c, S.count(c)) for c in chars)
    l = len(S) 
    if l < len(chars):
        return S + sorted(c for c, n in cs.items() if n == 0)[0]
    for i in range(2, l + 1):
        sl = S[:l - i]
        sc = S[l - i]
        sr = S[l - i + 1:]
        msrs = [c for c in sr if c > sc]
        if msrs:
            return sl + sorted(msrs)[0]
    return -1

S = input()
print(solve(S))

Solution (Python):
S = input()
if len(S) < 26:
    for i in range(26):
        c = chr(i + ord('a'))
        if c not in S:
            print(S + c)
            break
else:
    tmp = 26
    for i in range(len(S) - 1, 0, -1):
        if ord(S[i - 1]) < ord(S[i]):
            break
        tmp = i
    tmp -= 1
    if tmp == 0:
        print(-1)
    else:
        c = ord(S[:tmp][-1])
        suf = S[tmp:]
        print(S[:tmp - 1] + chr(min([ord(si) for si in suf if ord(si) > c])))

Solution (Python):
import string

S = input()
reversedS = ''.join(list(S)[::-1])
alphabets = list(string.ascii_lowercase)
lastWord = ''.join(alphabets[::-1])
cnt = [S.count(chr(ord('a')+i)) for i in range(26)]
if len(S) == 26:
    if S == lastWord: print(-1)
    else:
        found = False
        for i in range(len(reversedS)):
            s = reversedS[i]
            largeS = alphabets[ord(s)-ord('a')+1:]
            tailS = list(S)[26-i-1:]
            for lS in largeS:
                if lS in tailS:
                    print(S[:26-i-1]+lS)
                    found = True
                    break
            if found: break
else:
    for j in range(26):
        if cnt[j] == 0:
            S += chr(ord('a')+j)
            print(S)
            break

Solution (Python):
ars = "abcdefghijklmnopqrstuvwxyz"
S = input()
def iftasai(s):
  for i in s:
    if s.count(i) > 1:
      return False
  return True
if len(S) != 26:
  for i in ars:
    if not (i in S):
      print(S + i)
      quit()
elif S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  quit()
else:
  for i in range(1,len(S)+1):
    n = ars.index(S[-i])
    tmp = ars[n+1:]
    for j in tmp:
      ans = S[:-i] + j
      if iftasai(ans):
        print(ans)
        quit()

Solution (Python):
ars = "abcdefghijklmnopqrstuvwxyz"
S = input()
def iftasai(s):
  for i in s:
    if s.count(i) > 1:
      return False
  return True
if len(S) != 26:
  for i in ars:
    if not (i in S):
      print(S + i)
      quit()
elif S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  quit()
else:
  for i in range(1,len(S)+1):
    n = ars.index(S[-i])
    tmp = ars[n+1:]
    for j in tmp:
      ans = S[:-i] + j
      if iftasai(ans):
        print(ans)
        quit()
  n = ars.index(S[0])
  print(ars[n+1:])

Solution (Python):


c='abcdefghijklmnopqrstuvwxyz'
s=raw_input()
if len(s)<26:
 for v in c:
  if v not in s:
   res=s+v
   print res
   break
elif s==c[::-1]:
 print -1
else:
 s=s[::-1]
 i=0
 t=''
 while s[i]<s[i+1]:
  t+=s[i]
  i+=1
 t+=s[i]
 l=s[i+1:]
 r=ord('z')
 for v in t:
  if ord(v)>ord(l[0]):
   r=min(ord(v),r)
 res=chr(r)
 res=l[::-1][:-1]+res
 print res

Solution (Python):


eng_li = []
removed_list = []
all_letter = 26
for i in range (97,97+all_letter):
    eng_li.append(chr(i))
 
string = list(raw_input().strip())
length = len(string)
if length < all_letter:
    for i in string:
        eng_li.remove(i)
    string.append(eng_li[0])
    answer = "".join(string)
else:
    for i in range(all_letter-1):
        if ord(string[-1]) < ord(string[-2]) :
            removed_list.append(string[-1])
            string.pop()
        else:
            removed_list.append(string[-1])
            string.pop()
            removed_list=filter(lambda x:x>string[-1],sorted(removed_list))
            string.pop()
            string.append(removed_list[0])
            answer ="".join(string)
            break
    else:
        answer = -1
print answer