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):
ascii_lowercase='abcdefghijklmnopqrstuvwxyz'

s=input()

if(len(s)<26):
    for c in ascii_lowercase:
        if(s.find(c)<0):
            print(s+c)
            break
elif(s==ascii_lowercase[::-1]):
    print(-1)
else:
    for i in range(25):
        if(s[-i-2]<s[-i-1]):
            t=''.join(sorted(s[-i-2:]))
            j=t.find(s[-i-2])+1
            print(s[:-i-2]+t[j])
            break

Solution (Python):
s=input()
t=list(s)
u=list(s)
a=[]
for i in range(26):
    a.append(chr(i+97))
if len(t)!=26:
    flag=True
    for i in range(len(a)):
        if a[i] not in t:
            b=a[i]
            flag=False
            break

    if flag:
        print(-1)
    else:
        print(s+a[i])
else:
    flag2=True
    r=[t[-1]]
    t.remove(t[-1])
    while len(t):

        if r[-1]>t[-1]:
            k=t[-1]
            t.remove(t[-1])
            for i in range(len(r)):
                if r[i]>k:
                    ans=r[i]
                    break

            print("".join(t)+ans)
            flag2=False
            break
        if len(t)==0: break
        r.append(t[-1])
        t.remove(t[-1])

        r=sorted(r)
    if flag2: print(-1)

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

P = set(S)

flag = 0
if len(S) == 26:
    Q = set()
    for i in range(25):
        if ord(S[25 - i]) < ord(S[25 - i - 1]):
            Q.add(ord(S[25 - i]))
            S.pop()
        else:
            while len(Q)>0 and ord(S[25 - i - 1])>min(Q):
                Q.remove(min(Q))
            if len(Q)>0:
                S.pop()
                S.pop()
                S.append(chr(min(Q)))
                flag = 1
                break
            else:
                Z = S[25-i]
                S.pop()
                S.pop()
                S.append(Z)
                flag = 1
                break
else:
    for j in range(26):
        M = chr(97 + j)
        if M in P:
            pass
        else:
            S.append(M)
            flag = 1
            break

if flag == 0:
    print(-1)
else:
    print(''.join(S))

Solution (Python):
S= input()
alpha = [chr(i) for i in range(ord('a'), ord('z')+1)]
used = [0] * 26
for i in range(len(S)):
    for j in range(26):
        if S[i] == alpha[j]:
            used[j] = 1
            break
if 0 not in used:
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        remvid = []
        for x in range(25,0,-1):
            remvid.append(alpha.index(S[x]))
            if max(remvid) > alpha.index(S[x-1]):
                remvid.sort()
                tmp = 0
                while remvid[tmp] < alpha.index(S[x-1]):
                    tmp += 1
                print(S[:x-1]+alpha[remvid[tmp]])
                break
            else:
                continue
else:
    for x in range(26):
        if used[x] == 0:
            print(S+alpha[x])
            break

Solution (Python):


s = input()

alpha = [0] * 26




for c in s:
    i = ord(c)-97
    alpha[i] = 1
        

flag = True
for i,e in enumerate(alpha):
    if e == 0:
        s += chr(i + 97)
        flag = False
        break


if flag:
    i = len(s) - 1
    while i >= 1:
        alpha[ord(s[i])-97] = 0
        if s[i] > s[i-1]:
            
            s = s[:i]
            break
        i -= 1
    
    if i == 0:
        print(-1)
    else:
        
        
        for i,e in enumerate(alpha):
            if e == 0 and i > (ord(s[-1])-97):
                s = s[:-1]
                s += chr(i + 97)
                print(s)
                break
else:
    print(s)

Solution (Python):
import numpy as np

s = input()

text = "abcdefghijklmnopqrstuvwxyz"
count = np.zeros(len(text), dtype=np.int)

if(len(s) != len(text)):
    for i in s:
        for j in range(len(text)):
            if(i == text[j]):
                count[j] = 1
                break

    for i in range(len(text)):
        if(count[i] == 0):
            print((s + text[i]))
            exit()
else:
    for i in range(1, len(text) + 1, 1):
        count = np.zeros(len(text), dtype=np.int)
        for j in s[0:-i]:
            for k in range(len(text)):
                if(j == text[k]):
                    count[k] = 1
                    break

        for j in range(len(text)):
            if(s[-i] == text[j]):
                num = j
                break
        for j in range(num+1, len(text), 1):
            if(count[j] == 0):
                print((s[0:-i] + text[j]))
                exit()

print("-1")

Solution (Python):
s = input()
d = {0: [], 1:[]}
letters = 'abcdefghijklmnopqrstuvwxyz'
for i in letters:
    d[0].append(i)

curs = ''
ans = []
next = s[0]
c = []
f = False

for i in s:
    curs += i
    d[1].append(d[0].pop(d[0].index(i)))

if len(s) < 26:
    ans = curs + d[0][0]
else:
    while ans == []:
        if s:
            c.append(s[-1])
            s2 = s[:-1]
            c.sort()
            for j in c:
                if s2 + j > s:
                    ans = s2 + j
                    break
            s = s[:-1]
        else:
            ans = -1


print(ans)

Solution (Python):
a = input()
b = ["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"]
c = []
d = ["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"]
e = set()
for i in a:
    b.remove(i)
if len(b) > 0:
    print(a+b[0])

else:
    try:
        for j in a:
            c.append(j)
        for l in reversed(range(26)):
            e.add(c[l])
            del c[l]
            if len(c) == 0:
                print(-1)
                raise Exception
            memo = c[l-1]
            for k in range(len(d) - 1):
                if c[l - 1] == d[k]:
                    c[l - 1] = d[k + 1]
                    if c[l - 1] in e:
                        print("".join(c))
                        raise Exception
            c[l-1] = memo
    except Exception:
        pass

Solution (Python):
s=input()

ss=[0 for i in range(26)]
for i in range(len(s)):
    for j in range(26):
        if s[i]==chr(97+j):
            ss[j]=1

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





else:
    if s=="zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        for i in range(25):
            if s[24-i] < s[25-i]:
                s=s[:25-i]
                break

        sss=[0 for i in range(26)]
        for i in range(len(s)):
            for j in range(26):
                if s[i]==chr(97+j):
                    sss[j]=1

        for i in range(26):
            if sss[i]==0 and chr(97+i)> s[len(s)-1]:
                s=s[:len(s)-1]+chr(97+i)
                break

        print(s)

Solution (Python):
import sys


s = input()
alpha = [chr(i) for i in range(97, 97+26)]
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    res = ""
    for i in alpha:
        if i not in s:
            res = i
            break
    if res != "":
        print(s+res)
        sys.exit()

    tmp_res = s
    for i in s[::-1]:
        if i == "z":
            tmp_res = tmp_res[:-1]
        elif i != "z":
            for j in alpha[alpha.index(i)+1:]:
                if tmp_res.count(j) == 0:
                    print(tmp_res.replace(i, j))
                    sys.exit()
            tmp_res = tmp_res[:-1]

Solution (Python):
def Next(S):
    if len(S)!=26:
        f=[0 for i in range(26)]
        for i in S:
            f[ord(i)-97]+=1
        t=""
        for i in range(26):
            if f[i]==0:
                t=chr(i+97)
                break
        return (S+t)
    else:
        if S=="zyxwvutsrqponmlkjihgfedcba":
            return (-1)
        else:
            k=0
            for i in range(25):
                if ord(S[25-i])>ord(S[24-i]):
                    k=24-i
                    break
            T=S[:k]
            g=[0 for i in range(26)]
            for i in T:
                g[ord(i)-97]=1
            for n in range(ord(S[k])-97):
                g[n]=1
            g[ord(S[k])-97]=1
            t=""
            
            for i in range(26):
                if g[i]==0:
                    t=chr(i+97)
                    break
            return (T+t)
            return S
s=input()
print(Next(s))
'''
print("
for i in range(100):
    print(s)
    s=Next(s)
'''

Solution (Python):
s = input()
muximum = 'zyxwvutsrqponmlkjihgfedcba'
if s == muximum:
    print(-1)
else:
    if len(s) == 26:
        d = {c:0 for c in s}
        for i, c in enumerate(reversed(s)):
            for x in range(ord(c), ord('z') + 1):
                if chr(x) not in d:
                    break
            else:
                del d[c]
                continue
            break
        print(s[:-i-1] + chr(x))
    else:
        diff = set(muximum) - set(s)
        print(s + sorted(diff)[0])

Solution (Python):
s = input()

lst = ['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']

dic = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6,
       'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13,
       'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20,
       'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

if len(s) < 26:
    for x in s:
        lst.remove(x)
    print(s + lst[0])
    
else:
    tmp = ''
    cnt = 0
    cond = False
    for i in range(26):
        cnt_tmp = dic[s[-1]]
        if cnt <= cnt_tmp:
            tmp += s[-1]
            cnt = cnt_tmp
            s = s[:-1]
        else:
            last = s[-1]
            tmp += last
            s = s[:-1]
            cond = True
            break
            
    if cond:
        print(s + sorted(tmp)[sorted(tmp).index(last) + 1])
    else:
        print(-1)

Solution (Python):
s=list(raw_input())
l=len(s)-1
if len(s)<26:
    al = {}
    for i in s:
        al[i] = 1
    while (l >= 0):
        for i in range(97, 97 + 26):
            x = chr(i)
            if al.get(x, -1) == -1 :
                ans = s[:l + 1] + [x]
                if len(s) == 26:
                    ans[-2] = ''
                print ''.join(ans)
                exit()
        al[s[l]] = -1
        l -= 1
    print -1
else:
    al={}
    for i in s:
        al[i]=1
    while(l>=0):
        for i in range(97,97+26):
            x=chr(i)
            if al.get(x,-1)==-1 and x>s[l]:
                ans=s[:l+1] +[x]
                if len(s)==26:
                    ans[-2]=''
                print ''.join(ans)
                exit()
        al[s[l]]=-1
        l-=1
    print -1

Solution (Python):
def inc(s):
	ans = s[:-1]
	
	
	ans += chr(ord(s[-1]) + 1)
	return ans


s = input()





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

for l in w:
	if l not in s:
		print(s + l)
		exit(0)


for i in range(1, 26):
	
	ans = s[:-i]
	
	
	for l in w:
		if l not in ans[:-1] and ord(l) > ord(ans[-1]):
			print(ans[:-1] + l)
			exit(0)
	
print(-1)

Solution (Python):
from sys import stdin
from string import ascii_lowercase
from bisect import bisect_left


def main():
    word = stdin.readline().rstrip()
    if word == ascii_lowercase[::-1]:
        print(-1)
        return

    spam = sorted(list(set(ascii_lowercase) - set(word)))
    if 1 <= len(spam):
        print(word + spam[0])
        return

    rword = word[::-1]
    tmp = []
    for x in rword:
        if len(tmp) != 0 and x < sorted(tmp)[-1]:
            idx = bisect_left(tmp, x)
            print(word[:-len(tmp) - 1] + sorted(tmp)[idx])
            return
        tmp.append(x)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

S = input()

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(S) == 26:
    for i in range(26):
        for j in range(ord(S[-i - 1]) + 1, 97 + 26):
            if chr(j) not in S[:-i - 1]:
                print(S[:-i - 1] + chr(j))
                sys.exit()
else:
    for i in range(97, 97 + 26):
        if chr(i) not in S:
            print(S + chr(i))
            break

Solution (Python):
from string import ascii_lowercase


def solve(s):
    if len(s) < 26:
        for c in ascii_lowercase:
            if c not in s:
                return s + c

    while s:
        pi = ascii_lowercase.index(s[-1])
        s = s[:-1]
        for c in ascii_lowercase[pi + 1:]:
            if c not in s:
                return s + c
    return -1


s = input()
print(solve(s))

Solution (Python):
S = input().rstrip()

alphabet = list('abcdefghijklmnopqrstuvwxyz')


if len(S)<26:
    A = [a for a in alphabet if not a in S]
    print(S + A[0])
    exit()


for i in range(24, -1, -1):
    A = [a for a in alphabet if not a in S[:(i+1)]]
    for a in A:
        if S[i]<a:
            print(S[:i] + a)
            exit()


print(-1)

Solution (Python):
S = input()

alphabets = [chr(i) for i in range(97, 97+26)]
str_len = len(S)

if str_len < 26:
    for alphabet in alphabets:
        if alphabet not in S:
            print(S + alphabet)
            exit()
else:
    for i in range(-1, -str_len, -1):
        tmp_string = sorted(list(S[i:]))
        for tmp_str in tmp_string:
            if tmp_str > S[i - 1]:
                print(S[:i - 1] + tmp_str)
                exit()
print(-1)

Solution (Python):
S = input()
frag = False
for i in range(97, 123):
    if chr(i) not in S:
        print("".join([S, chr(i)]))
        frag = True
        break
if frag == False:
    for i in range(2, len(S) + 1):
        for j in range(97, 123):
            if chr(j) not in S[:1-i] and j > ord(S[1-i]):
                frag = True
                break
        if frag:
            break
    if frag:
        print("".join([S[:1-i], chr(j)]))
    else:
        if S[0] == "z":
            print(-1)
        else:
            print(chr(ord(S[0]) + 1))

Solution (Python):

def find_word():
    word = input()
    alpha = [chr(i) for i in range(97, 97+26)]
    alpha_set = set(alpha)
    word_set = set(list(word))
    if alpha_set - word_set:
        for w in alpha:
            if not w in word:
                return (word + w)
    else:
        for i, w in enumerate(word[::-1]):
            new_word = word[:(-1 * (i + 1))]
            for j in range(1, 27):
                if 97 <= ord(w) + j <= 122 and not chr(ord(w) + j) in new_word:
                    return (new_word + chr(ord(w) + j))
    return -1

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

Solution (Python):

import string
s = input()
rem = set(string.ascii_lowercase).difference(set(s))
answer = -1
for r in reversed(range(len(s) + 1)):
    if r < len(s):
        rem.add(s[r])
    for c in rem:
        t = s[: r] + c
        if s < t and (answer == -1 or t < answer):
            answer = t
print(answer)

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

if len(S) < 26:
  for i in range(len(alp)):
    c = alp[i]
    index = S.find(c)
    if index <0 :
      print(S,c,sep='')
      exit()

stack=[]
c_pre=""
for j in range(len(S)):
  c_now = S[25-j]
  
  if c_now < c_pre :
    S = S[:-j-1]
    
    c_min = "z"
    for cx in stack:
      if c_now < cx and cx <= c_min :
        c_min = cx
        
    print(S,c_min,sep='')
    exit()
  else :
    stack.append(c_now)
    c_pre = c_now

print("-1")

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

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

    if len(S) < 26:
        for i in range(97, 123):
            if chr(i) not in S:
                print(S + chr(i))
                break
    else:
        for i in range(24, -1, -1):
            if S[i] != 'z':
                s = ord(S[i]) + 1
                for j in range(s, 123):
                    if chr(j) not in S[:i]:
                        print(S[:i] + chr(j))
                        return

main()

Solution (Python):
def ctoi(c):
    return ord(c) - 97


def itoc(i):
    return chr(i + 97)


def ans(S):
    l = len(S)
    if l != 26:
        alpha = [False] * 26
        for i in range(l):
            alpha[ctoi(S[i])] = True
        for j in range(26):
            if not alpha[j]:
                ret = S + itoc(j)
                return ret
    else:
        alpha = [True] * 26
        for i in range(l):
            if i == l-1:
                ret = str(itoc(ctoi(S[0])+1))
                
                
                
                
                
                
                
                return ret
            else:
                for j in range(ctoi(S[l-i-1])+1, 26):
                    if not alpha[j]:
                        ret = S[:l-i-1] + itoc(j)
                        
                        
                        
                        
                        
                        
                        
                        return ret
                alpha[ctoi(S[l-i-1])] = False


S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    print(ans(S))

Solution (Python):
from collections import defaultdict
from string import ascii_lowercase

s = input()
if s == 'zyxwvutsrqponmlkjihgfedcba' :
    print(-1)
elif len(s) < 26 :
    s = list(s)
    d = defaultdict(int)
    for c in s :
        d[c] += 1
    for c in ascii_lowercase :
        if d[c] == 0 :
            s.append(c)
            print(''.join(s))
            break
elif len(s) == 26 :
    s = list(s)
    d = defaultdict(int)
    for i in range(25, 0, -1) :
        if s[i-1] < s[i] :
            d[s[i]] = 1
            for c in ascii_lowercase :
                if d[c] == 1 and s[i-1] < c :
                    ans = s[0:i-1]
                    ans = ans + [c]
                    print(''.join(ans))
                    exit()
        elif s[i-1] > s[i] :
            d[s[i]] = 1

Solution (Python):
def Al(p):
   Alp = "abcdefghijklmnopqrstuvwxyz"
   for j in range(26):
      if p == Alp[j]:
         return j

def main():
   S = input()

   alpha = "abcdefghijklmnopqrstuvwxyz"

   if S == "zyxwvutsrqponmlkjihgfedcba":
      return -1

   elif len(S) != 26:
      for a in alpha:
         if (a in S)==False:
            return S + a
   
   else:
      A = ""
      for i in range(len(S)):
         s = S[-(i+1)]
         Snew = S[:-(i+1)]
         for a in A:
            if Al(s) < Al(a):
               return Snew + a
         A = A + s

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

Solution (Python):
def main():
    S = input()
    if len(S) >= 26:
        for i in range(24, -1, -1):
            if S[i] < S[i+1]:
                m = 'z'
                for j in S[i+1:]:
                    if j > S[i] and j < m:
                        m = j
                print(S[:i]+m)
                return
        print(-1)
    else:
        for i in range(ord('a'), ord('z')+1):
            flag = True
            for j in S:
                if j == chr(i):
                    flag = False
                    break
            if flag:
                print(S+chr(i))
                return


if __name__ == '__main__':
    main()

Solution (Python):
alph = ['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']
word = list(input())

achars = alph[:]
for char in word:
    achars.remove(char)

if len(word) < 26:
    print(''.join(word)+achars[0])
else:
    ind = 26
    for i in range(1,26):
        if alph.index(word[i]) > alph.index(word[i-1]):
            ind = i
    if ind == 26:
        print(-1)
    else:
        word = word[:ind]
        for char in alph:
            if alph.index(char) > alph.index(word[ind-1]) and char not in word:
                word[ind-1] = char
                break
        print(''.join(word))

Solution (Python):


ABC = "abcdefghijklmnopqrstuvwxyz"

def add_next(s):    
    for c in ABC:
        if c not in s:
            return s + c
    raise Exception

def change_next(s):
    for i in reversed(range(n)):
        flag = False
        for j in range(i + 1, n):
            if ord(s[i]) < ord(s[j]):
                flag = True
                break
        if flag:
            for k in range(1, n):
                if chr(ord(s[i]) + k) not in s[:i]:
                    return s[:i] + chr(ord(s[i]) + k)
            raise Exception
    raise Exception

s = input()
n = len(s)

if n < 26:
    res = add_next(s)
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        res = "-1"
    else:
        res = change_next(s)

print(res)

Solution (Python):
import string

S = input()
N = len(S)

if S == string.ascii_lowercase[::-1]:
    print(-1)
elif N <= 25:
    for l in string.ascii_lowercase:
        if l not in S:
            print(S + l)
            break
else:
    ret = string.ascii_lowercase[::-1]
    for i in range(N):
        for c in string.ascii_lowercase:
            if c in S[:i]:
                continue
            s = S[:i] + c
            if S >= s:
                continue
            ret = min(ret, s)
    print(ret)

Solution (Python):


S=input()
L=len(S)+1

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


flag=0

S=S+'A'
for i in range(L):
    for letter in ascii_lowercase:
  
        if (letter not in S[:L-i-1]) and letter > S[L-i-1]:
            flag = 1
            answer = S[:L-i-1]+letter
            break
    if flag==1:
        break

if flag==1:
    print(answer)
else:
    print('-1')

Solution (Python):
import string

x = input()
if x == "zyxwvutsrqponmlkjihgfedcba":
	print(-1)
else:
	lis = list(x)
	letter = list(string.ascii_lowercase)
	
	if len(lis) != 26:
		for item in letter:
			if not item in lis:
				print(x+item)
				break
	else:
		n = 25
		box = []
		while n > 0:
			if lis[n] > lis[n-1]:
				box.append(lis.pop(n))
				set = "z"
				for st in box:
					if lis[n-1] < st and set >= st:
						set = st
				lis.pop(n-1)
				print("".join(lis)+set)
				break
			else:
				box.append(lis.pop(n))
			n -= 1

Solution (Python):
S = input()

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

    code = list(map(lambda x : ord(x) - ord('a'), list(S)))

    done = False
    for i in range(26):
        if not i in code:
            code.append(i)
            done = True
            break


    while len(code) > 0 and not done:
        last = code[-1]
        code = code[:-1]
        if last == 25:
            continue
        else:
            for i in range(last+1, 26):
                if i not in code:
                    code.append(i)
                    done = True
                    break


    decode = list(map(lambda x : chr(x + ord('a')), code))
    print(''.join(decode))

Solution (Python):
s = input()
s2 = list(s)
ch = [chr(i) for i in range(97, 97+26)]
flg = 0
for k in ch:
	if not k in s2:
		s += k
		flg = 1
		break

if flg == 1:
	print(s)
	exit(0)
else:
	l = len(s)
	ans = ''
	for i in range(l-1,0,-1):
		for k in ch:
			if k <= s[i]:
				continue
			elif not k in s2[:i]:
				ans = s[:i] + k
				print(ans)
				exit(0)

	if s[0] == 'z':
		print(-1)
	else:
		print(chr(ord(s[0])+1))

Solution (Python):
S0 = input()
S = S0
C = set("abcdefghijklmnopqrstuvwxyz")
C1 = set(S)
candidate = sorted(C - C1)
if candidate != []:
    if S0 < S + candidate[0]:
        print(S + candidate[0])
        exit()
while S != "":
    C1 = set(S)
    candidate = sorted([ c for c in C - C1 if c > S[-1]])
    if candidate != []:
        if S0 < S[0:-1] + candidate[0]:
            print(S[0:-1] + candidate[0])
            exit()
    S = S[0:- 1]
if S0[0] == "z":
    print(-1)
else:
    print(chr(ord(S0[0]) + 1))

Solution (Python):
def _agc022_a():
    S = input()
    al = 'abcdefghijklmnopqrstuvwxyz'
    if len(S) < 26:
        for c in al:
            if not c in S:
                print(S+c)
                return
    elif S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        T = S
        A = [T[-1]]
        T = T[:-2]

        while True:
            for c in A:
                if S < T+c:
                    print(T+c)
                    return

            A += T[-1]
            T = T[:-1]

_agc022_a()

Solution (Python):



def main(s):
	alphabet = "abcdefghijklmnopqrstuvwxyz"
	d = {alphabet[i]:i for i in range(26)}
	if s == "zyxwvutsrqponmlkjihgfedcba":
		print("-1")
	elif len(s) < 26:
		for i in range(26):
			if alphabet[i] not in s:
				print(s + alphabet[i])
				break
	else:
		tail = [d[s[25]]]
		for i in range(0, 25):
			if d[s[24 - i]] >= max(tail):
				tail.append(d[s[24 - i]])
				continue
			else:
				tmp = min([x for x in tail if x >= d[s[24-i]]])
				print(s[0:(24-i)] + alphabet[tmp])
				break


if __name__ == '__main__':
	s = input()
	main(s)

Solution (Python):
S = input()
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 i not in S:
        print(S + i)
        break
else:
    output = False
    while len(S) > 0 and not output:
        last = alphabet.index(S[-1])
        for i in alphabet[last + 1:]:
            if i not in S[:-1]:
                print(S[:-1] + i)
                output = not(output)
                break
        else:
            S = S[:-1]
    if not output:
        print(-1)

Solution (Python):
from string import lowercase
s = raw_input().strip()
if len(s) < 26:
    for c in lowercase:
        if c not in s:
            print s + c
            break
else:
    c = ' '
    for i in xrange(25, -1, -1):
        if s[i] < c:
            s = s[:i+1]
            for c in lowercase:
                if c > s[-1] and c not in s:
                    print s[:-1] + c
                    quit()
        c = s[i]
    print -1

Solution (Python):
S = input()

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

if len(S) == 26:
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        for i in range(1, 26):
            for j in abc[abc.index(S[-(i+1)])+1:]:
                if j in S[-i:]:
                    print(S[:-(i+1)]+j)
                    end = 1
                    break
            if end == 1:
                break
        else:
            print(-1)
                
    
else:
    for i in S:
        abc.remove(i)
    print(S+abc[0])

Solution (Python):

def inpl(): return tuple(map(int, input().split()))
from collections import Counter
from string import ascii_lowercase

S = input()
C = Counter(list(S))
D = {k:i for i, k in enumerate(ascii_lowercase)}

for a in list(ascii_lowercase):
    if C[a] == 0:
        S += a
        print(S)
        break
else:
    nokori = [D[S[-1]]]    
    for i in range(24, -1, -1):
        res = 30
        for n in nokori:
            if D[S[i]] < n:
                res = min(res, n)

        if res == 30:
            nokori.append(D[S[i]])            
        else:
            S = S[:i] + ascii_lowercase[res]
            print(S)
            break
    else:
        print(-1)

Solution (Python):
j = input()
lis = sorted(list("qwertyuiopasdfghjklzxcvbnm"))
abs = ""
for h in j:
    lis.remove(h)
if not lis:
    ans = "-1"
else:
    ans = "{}{}".format(j, lis[0])
tmp = "zz"
for i in range(len(j)):
    try:
        if tmp > j[:i] + sorted(filter(lambda x: x > j[i], j[i+1:]))[0]:
            tmp = j[:i] + sorted(filter(lambda x: x > j[i], j[i+1:]))[0]
    except:
        pass
if tmp == "zz" and ans == "-1":
    print("-1")
elif tmp == "zz":
    print(ans)
elif ans == "-1":
    print(tmp)
else:
    print(min(ans, tmp))

Solution (Python):
from collections import Counter
S = input()
ct = Counter(S)
if len(ct) != 26:
  for i in range(26):
    c = chr(ord('a')+i)
    if c not in ct:
      print(S+c)
      exit()

prev = chr(ord('a')-1)
arr = []
for i in range(26)[::-1]:
  if prev > S[i]:
    print(S[:i] + min(filter(lambda x: x > S[i],arr)))
    break
  arr.append(S[i])
  prev = S[i]
else:
  print(-1)

Solution (Python):
def main():
    s = input().strip()
    if len(s) < 26:
        for i in range(ord('a'), ord('z') + 1):
            c = chr(i)
            if c not in s:
                print(s + c)
                return
    for i in range(24, -1, -1):
        c = s[i]
        ss = s[:i]
        for j in range(ord(c) + 1, ord('z') + 1):
            cc = chr(j)
            if cc not in ss:
                print(ss + cc)
                return
    print(-1)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
if s == 'zyxwvutsrqponmlkjihgfedcba':
  print(-1)
  exit()
L = 26
s = [ord(c) - ord('a') for c in s]


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

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

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

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

Solution (Python):
INF = 999999999999999999L
EPS = 1e-12

def read():
    return raw_input().strip()

def read_ints():
    return map(int,read().split())

s = read()
if len(s) < 26:
    for i in range(26):
        c = chr(ord("a")+i)
        if c not in s:
            print s+c
            break 
            
else:
    for i in reversed(range(len(s))):
        c = ord(s[i]) - ord("a")
        found = False
        for j in range(c+1,26):
            cc = chr(ord("a")+j)
            if cc not in s[:i]:
                print s[:i]+cc 
                found = True 
                break 
        if found:
            break
    if not found:
        print -1

Solution (Python):
S = raw_input()
A = [chr(ord("a")+i) for i in range(26)]
if len(S)<26:
  for a in A:
    if a not in S:
      print S+a
      exit(0)
else:
  for p in range(26)[::-1]:
    for a in A[A.index(S[p])+1:]:
      if a not in S[:p]:
        print S[:p]+a
        exit(0)
  print -1

Solution (Python):
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
def hoge(string, least = 'Z'):
    for c in ALPHABET:
        if c not in string:
            if c <= least:continue
            return string + c
    if string == "":
        return "-1"
    return hoge(string[:-1], string[-1])

print hoge(raw_input())