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



Sample input:
atcoder


Sample output:
atcoderb

Submission status: Wrong Answer

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

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

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

appear = [0 for i in range(27)]
string = [0]
for i in range(n):
	string.append(d[s[i]])
	appear[d[s[i]]] += 1

exist = 0
for i in range(1,27):
	if appear[i] == 0:
		exist = i
		break

if exist != 0: #ない文字があった時
	string.append(exist)
	for k in range(1,n+2):
		if k != n+1:
			print(e[string[k]],end='')
		else:
			print(e[string[k]])

else: #全部ある時
	if s == 'zyxwvutsrqponmlkjihgfedcba':
		print('-1')
	else:
		bunki = 0
		bunki2 = []
		memo = []
		for i in range(1,26):
			if string[i] > string[i+1]:
				bunki = i
				bunki2.append(i)

		if bunki != 25:		
			temp = [0]
			temp[0] = string[25]
			string[25] = string[26]
			string[26] = temp[0]
			for h in range(1,27):
				if h != 26:
					print(e[string[h]],end='')
				else:
					print(e[string[h]])
			
		else:
			w = len(bunki2)
			ren = bunki2[w-1]
			while (ren-1) in bunki2:
				ren += -1
			taihi = [0]
			taihi[0] = ren-1 #文字列の番号
			memo = []
			ren += -1
			while ren<=26:
				memo.append(string[ren])
				ren += 1
			memo.sort()
			okikae = 0
			for k in range(len(memo)):
				if string[taihi[0]] < memo[k]:
					okikae = memo[k]
					break
			string[taihi[0]] = okikae
			for h in range(1,taihi[0]+1):
				if h != taihi[0]:
					print(e[string[h]],end='')
				else:
					print(e[string[h]])

Submission status: Wrong Answer

Solution code:
def solve(S):
    is_used = [False] * 26
    for s in S:
        is_used[ord(s) - ord("a")] = True

    S = S[::-1]
    cand = S[0]
    length = len(S)
    if length == 26:
        for i in range(length - 1):
            if ord(S[i]) > ord(S[i+1]):
                return (S[i+2:length])[::-1] + cand
    for i in range(26):
        if not is_used[i]:
            return S[::-1] + chr(i + ord('a'))
    return "-1"

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

Submission status: Wrong Answer

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

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

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

appear = [0 for i in range(27)]
string = [0]
for i in range(n):
	string.append(d[s[i]])
	appear[d[s[i]]] += 1

exist = 0
for i in range(1,27):
	if appear[i] == 0:
		exist = i
		break

if exist != 0: #ない文字があった時
	string.append(exist)
	for k in range(1,n+2):
		if k != n+1:
			print(e[string[k]],end='')
		else:
			print(e[string[k]])

else: #全部ある時
	if s == 'zyxwvutsrqponmlkjihgfedcba':
		print('-1')
	else:
		bunki = 0
		bunki2 = []
		memo = []
		for i in range(1,26):
			if string[i] > string[i+1]:
				bunki = i
				bunki2.append(i)

		if bunki != 25:		
			temp = [0]
			temp[0] = string[25]
			string[25] = string[26]
			string[26] = temp[0]
			for h in range(1,27):
				if h != 26:
					print(e[string[h]],end='')
				else:
					print(e[string[h]])
			
		else:
			w = len(bunki2)
			ren = bunki2[w-1]
			print(bunki2)
			while (ren-1) in bunki2:
				ren += -1
			print(ren)
			taihi = [0]
			taihi[0] = ren-1 #文字列の番号
			memo = []
			ren += -1
			while ren<=26:
				memo.append(string[ren])
				ren += 1
			memo.sort()
			okikae = 0
			for k in range(len(memo)):
				if string[taihi[0]] < memo[k]:
					okikae = memo[k]
					break
			string[taihi[0]] = okikae
			for h in range(1,taihi[0]+1):
				if h != taihi[0]:
					print(e[string[h]],end='')
				else:
					print(e[string[h]])

Submission status: Wrong Answer

Solution code:
S = raw_input()

lis = 'abcdefghijklmnopqrstuvwxyz'

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

if len(S) != 26:
    for i in S:
        k = 0
        while i != lis[k]:
            k += 1
        exs[k] = 1
    j = 0
    
    while exs[j] == 1:
        j += 1
    print S + lis[j]

else:
    k = 0
    while S[len(S) - 1] != lis[k]:
        k += 1
    s = k
    while s >= k:
        s -= 1
    if s != -1:
        S = S[:s]
        for i in S:
            l = 0
            while i != lis[l]:
                l += 1
            exs[l] = 1
        j = 0
        while exs[j] == 1:
            j += 1
        print S + lis[j]
    else:
        print s

Submission status: Wrong Answer

Solution code:
s = input()

ax = set()

a = 'zyxwvutsrqponmlkjihgfedcba'

ss = s[:]

if s == a:
    print('-1')
    exit(0)

for c in a:
    ax.add(c)

def f(ss):
    x = set()
    for c in ss:
        x.add(c)
    return ax ^ x

def g(s, xx):
    return s + sorted(list(xx))[0]

if s == a:
    print('-1')
else:
    xx = f(s)
    if len(xx) != 0:
        print(g(s, xx))
    else:
        s = list(s)
        c = s[-1]
        del s[-1]
        xx = f(s)
        cc = c
        while len(s) > 0 and s[-1] >= sorted(list(xx))[0]:
            cc = s[-1]
            del s[-1]
            xx = f(s)
        if len(s) == 0:
            s = chr(ord(cc) + 1)
            s = ''.join(s)
            print(s)
        else:
            xx = f(s)
            s = ''.join(s)
            print(g(s, xx))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# AtCoder Grand Contest
# Problem A

if __name__ == '__main__':
    s = list(input())
    alphabets = [chr(ord('a') + i) for i in range(26)]

    if len(s) == 26:
        if s == alphabets[::-1]:
            print(-1)
        else:
            result = s[0]
            hoge = alphabets[alphabets.index(result) + 1:]

            for i in range(1, len(hoge)):
                if s[i] in hoge:
                    # hoge = hoge[hoge.index(s[i]) + 1:]
                    # result += s[i]
                    exit()
                else:
                    print(result + hoge[0])
                    exit()
    else:
        sorted_s = sorted(s)
        diff = set(alphabets) - set(sorted_s)
        candidate = sorted(diff)[0]
        print(''.join(s + [candidate]))

Submission status: Wrong Answer

Solution code:
import string
s = input()
s2=s.lower()
alp = string.ascii_lowercase
ans=''
def diff(a, b):
    if a=='a' or b=='a':
        if a=='z' or b=='z':
            return 1
    if a=='z' or b=='z':
        if a=='a' or b=='a':
            return 1
    return abs(alp.index(a)-alp.index(b))
if len(s)<26:
    for i in range(26):
        if alp[i] not in s2:
            ans=alp[i]
            break
    if ans=='':
        print(-1)
    else:
        print(s+ans)
        
else:
    found = False
    ans=-1
    for i in range(1,26):
        if diff(s2[i], s2[i-1])>1:
            found=True
            ans=i
            break

    if not found:
        if s2[0]<s2[1]:
            for j in range(25, -1, -1):
                if s2[j-1]<s2[j]:
                    J=j
                    break
            temp=s2[J]
            t2=s2[J-1]
            s2=s2[:j-1]+temp+t2+s2[J+1:]
            print(s2)
        else:
            can=False
            for i in range(1, 26):
                if s2[i]=='z' and s2[i-1]=='a':
                    if list(s2[:i])==sorted(list(s2[:i]))[::-1] and list(s2[i:])==sorted(list(s2[i:]))[::-1]:
                        print(s2[:i-1]+s2[i:]+'a')
                        can=True
                        break
                                            
            if not can and s2[0]=='z':
                print(-1)
            elif not can:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        if s2[ans:] in alp[alp.index(s2[ans-1]):][::-1]:
            s2=s2[:ans+1]
            if s[ans]=='z':
                char=alp[alp.index(s[ans-1])+1]
                s2=s2[:ans]+char
            else:
                char=alp[alp.index(s[ans])+1]
                s2=s[:ans]+char
            print(s2)
        else:
            for j in range(25, -1, -1):
                if s2[j-1]<s2[j]:
                    J=j
                    break
            temp=s2[J]
            t2=s2[J-1]
            s2=s2[:j-1]+temp+t2+s2[J+1:]
            print(s2)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


a_code = 97
z_code = 122

def main(text):
    ret = text
    if text == 'zyxwvutsrqponmlkjihgfedcba':
        return -1
    l = len(text)
    ls = range(l)
    i = a_code
    while i <= z_code:
        flag = False
        for j in ls:
            if ord(text[j]) == i:
                break
        if j == l - 1 and ord(text[j]) != i: 
            break
        i = i + 1
    if i == 123:
        if text[0] == "a":
            abc = "abcdefghijklmnopqrstuvwxyz"
            for i in range(len(text)):
                if abc[i] == text[i]:
                    continue
                return abc[:i-1]+chr(ord(text[i-1])+1)
        else:
            abc = 'zyxwvutsrqponmlkjihgfedcba'
            for i in range(len(text)):
                if abc[i] == text[i]:
                    continue
                return abc[:-1]
        return -1
    else:
        return ret + chr(i)

if __name__ == "__main__":
    s = raw_input()

    print(main(s))

Submission status: Wrong Answer

Solution code:
alphabet='abcdefghijklmnopqrstuvwxyz'

def next_word(S,alphabet):
    if len(S)==0:
        return -1
    ind=S.find(alphabet[-1])
    if len(S)<len(alphabet):
        S1=alphabet[:]
        for s in S:
            S1=S1.replace(s,'')
        S1=list(S1)
        S1.sort()
        return S+S1[0]

    elif ind>0:
        S1=list(S[ind:])
        S1.sort()
        return S[:ind-1]+S1[0]

    else:
        word=next_word(S[1:],alphabet[:-1])
        if word==-1:
            return -1
        else:
            return S[0]+word

S=input()

print(next_word(S,alphabet))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr  1 00:03:14 2018

@author: Kim
"""

S = input()
Slist = list(S)

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
Alpha = list(ascii_lowercase)

alpha_dict = {chr(c):i+1 for i,c in enumerate(range(ord('a'),ord('z')+1))}


ANS = []
ANSm = ""

#クソ
if S == "abcdefghijklmnopqrstuvwxyz":
    print("b")
elif S == "zyxwvutsrqponmlkjihgfedcba":
    print("-1")
    
#全部使われない場合
else:
    for i in range(len(Slist)):
            if Slist[i] in alpha_dict:
                del alpha_dict[Slist[i]]
    if alpha_dict == {}:
        #全部使われる場合
        
        alpha_dict = {chr(c):i+1 for i,c in enumerate(range(ord('a'),ord('z')+1))}
        if Slist[0] == "z":
            DEL = alpha_dict[Slist[1]]
            ANS.append("z")
            ANS.append(Alpha[DEL])
            if ANS[1] == "z":
                ANS.pop()
                ANS[0] = "-1"
                
        else:
                
            for i in range(len(Slist)):
                if Slist[i] == "z":
                    ANS.pop()
                    DEL = alpha_dict[Slist[i-1]]
                    
                    ANS.append(Alpha[DEL])
                    break
                else:
                   ANS.append(Slist[i])
                   
        print( ''.join(ANS))
        
    else:
        minS = min([(v,k) for k,v in alpha_dict.items()])[1]
        print(S+minS)

Submission status: Wrong Answer

Solution code:
inp = input()
inp_len = len(inp)
chars = map(ord,list(inp))
dic = {}
for char in chars:
    dic[char] = "exist"
an = ""
if inp_len == 26:
    for i in range(25):
        if ord(inp[-1 -i]) > ord(inp[-2-i]):
            if i ==0:
                an = inp[:-2-i] + inp[-1-i] + inp[-2-i]
                break
            else:
                an = inp[:-2-i] + inp[-1-i] + inp[-2-i] + inp[-i:]
                break
    if an == "":
        an = "-1"
else:
    for i in range(26):
        if not (97 + i) in dic:
            an =  inp + chr(97+i)
            break
print(an)

Submission status: Wrong Answer

Solution code:
import sys


outp1="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
outp2="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
outp3="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
outp4="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

alph="abcdefghijklmnopqrstuvwxyz"
allis=list(alph)
S=input()


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


if(S=="abcdefghijklmnopqrstuvwxyz"):
    outp1="b"
    
for i in alph:
    if(i not in S):
        outp2=(S+i)
        break


outp=""
flag=0
for i in range(len(S)-1):
    if(ord(S[i])-ord(S[i+1])<-1):
        for j in alph:
            if(j not in S[:i+1]):
                outp=S[:i]+j
                outp3=(S[:i]+j)
                flag=1
                break
    if(flag):break
    elif(ord(S[i+1])-ord(S[i])<-1):
        for j in alph:
            if(j not in S[:i+2]):
                outp=S[:i+1]+j
                outp3=(S[:i+1]+j)
                flag=1
                break
    if(flag):break
        

D=S[::-1]
chan=-1
for i in range(len(D)-1):
    if(ord(D[i])>ord(D[i+1])):
        chan=len(S)-i
        break
if(chan!=-1):
    E=sorted(S[chan-1:])
    for i in range(len(E)):
        if(E[i]>S[chan-2]):
            b=E[i]
            E[i]=S[chan-2]
            x=list(S)
            x[chan-2]=b
            break
    outp4=("".join(x[:chan-1])+"".join(E))
    
print(min(outp1,outp2,outp3,outp4))

Submission status: Wrong Answer

Solution code:
s_lis = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
         'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v','w','x','y','z']
s_lis2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
         'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v','w','x','y','z']

S = 'abcdefghijklmnopqrstuvwzyx'

for i in range(len(S)):
    s_lis.remove(S[i])

if s_lis != []:
    print(S + s_lis[0])
else:
    S = S.split('z')[0][:-1]
    if S[-1] == 'y':
        print(S[-2] + 'z')
    if S == '':
        print(-1)
    else:
        for i in range(len(S)):
            s_lis2.remove(S[i])

        try:
            if s_lis2 != []:
                print(S+ s_lis2[1])
        except:
            print( -1)

Submission status: Wrong Answer

Solution code:
s = input()

ax = set()

a = 'zyxwvutsrqponmlkjihgfedcba'

ss = s[:]

if s == a:
    print('-1')
    exit(0)

for c in a:
    ax.add(c)

def f(ss):
    x = set()
    for c in ss:
        x.add(c)
    return ax ^ x

def g(s, xx):
    return s + sorted(list(xx))[0]

if s == a:
    print('-1')
else:
    xx = f(s)
    if len(xx) != 0:
        print(g(s, xx))
    else:
        s = list(s)
        c = s[-1]
        del s[-1]
        xx = f(s)
        while len(s) > 0 and s[-1] >= sorted(list(xx))[0]:
            del s[-1]
            xx = f(s)
        if len(s) == 0:
            s = c
        else:
            s[-1] = c
        s = ''.join(s)
        print(s)

Submission status: Wrong Answer

Solution code:
s = list(input())
if sorted(s, reverse=True) == s and len(s) == 26:
    print(-1)
    quit()
for i in range(1, 26+1):
    if chr(97 + i) not in s and len(s) != 26:
        print(''.join(s) + chr(97+i))
        quit()

while len(s) > 0:
    while ord(s[-1]) < ord('z'):
        s[-1] = chr(ord(s[-1])+1)
        if len(s) == len(set(s)):
            print(''.join(s))
            quit()
    del s[-1]

Submission status: Wrong Answer

Solution code:
S=input()

if len(S)<26:
    for i in range(97,123):
        if not(chr(i) in S):
            print(S+chr(i))
            break
else: #len=26
    if S=='zyxwvutsrqponmlkjihgfedcba':print(-1)
    elif S=='abcdefghijklmnopqrstuvwxyz':
        print('abcdefghijklmnopqrstuvwxzy')
    else:
        for i in range(25):
            if S[i]>S[i+1]:
                if S[i]=="z":
                    if i==0:
                        pass
                    else:
                        if i-1==0:end=0
                        else:end=i-1
                        newS=S[0:end]+chr(ord(S[i-1])+1)
                        break
                else:
                    newS=S[0:i]+chr(ord(S[i])+1)
                    break
        print(newS)

Submission status: Wrong Answer

Solution code:
D = [chr(i) for i in range(97, 97+26)]
rD = D[::-1]
S = input()
s = list(S)
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif len(S) <26:
    for i in s:
        D.remove(i)
    S += D[0]
    print(S)
else:
    zi = s.index("z")
    tS = s[:]
    del tS[0:zi]
    if ''.join(tS) in ''.join(rD):
        a = s[25]
        del s[zi-1:26]
        s.append(a)
        print(''.join(s))
    else:
        ii = 0
        a = s[25]
        i = 25
        for i in range(24,-1,-1):
            if ord(a) >= ord(s[i]):
                ii = i
                break
        del s[ii:26]
        s.append(a)
        print(''.join(s))

Submission status: Wrong Answer

Solution code:
dic = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
s = input()
dic_copy = dic.copy()
end = 0

for i in s:
    if i in dic:
        dic.remove(i)
    else:
        end = 1
        break

if end == 1:
    print(-1)
elif len(dic) == 0:
    if s[0] == 'z':
        print(-1)
    else:
        temp = ''
        for i in s:
            if i == 'z':
                break
            temp = temp + i
        
        print(temp[:-1]+dic_copy[temp.index(temp[-1])+1])
        
else :
    print(s+dic[0])

Submission status: Wrong Answer

Solution code:
ab=[False]*26
vch={'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}
ch=raw_input().strip().lower()
i="abcdefghijklmnopqrstuvwxyz"
if(ch==i or ch==i[::-1] or len(set(ch))!=len(ch)): print("%d"%(-1))
elif(len(ch)==26):
    for j in xrange(25,0,-1):
        if( abs(ord(ch[j])-ord(ch[j-1]))>1):
            idx=min(ord(ch[j]),ord(ch[j-1]))
            print("%s"%(ch[:j]+min(list(ch[j:]))))
            break                           
else:
  for j in ch:
      ab[vch[j]]=True
  for j in xrange(26):
      if(ab[j]==False):
          break
  print("%s"%(ch+i[j]))

Submission status: Wrong Answer

Solution code:
import string
s = input()
s2=s.lower()
alp = string.ascii_lowercase
ans=''
def diff(a, b):
    if a=='a' or b=='a':
        if a=='z' or b=='z':
            return 1
    if a=='z' or b=='z':
        if a=='a' or b=='a':
            return 1
    return abs(alp.index(a)-alp.index(b))
if len(s)<26:
    for i in range(26):
        if alp[i] not in s2:
            ans=alp[i]
            break
    if ans=='':
        print(-1)
    else:
        print(s+ans)
        
else:
    found = False
    ans=-1
    for i in range(1,26):
        if diff(s2[i], s2[i-1])>1:
            found=True
            ans=i
            break

    if not found:
        if s2[0]<s2[1]:
            for j in range(25, -1, -1):
                if s2[j-1]<s2[j]:
                    J=j
                    break
            temp=s2[J]
            t2=s2[J-1]
            s2=s2[:j-1]+temp+t2+s2[J+1:]
            print(s2)
        else:
            can=False
            for i in range(1, 26):
                if s2[i]=='z' and s2[i-1]=='a':
                    if list(s2[:i])==sorted(list(s2[:i]))[::-1] and list(s2[i:])==sorted(list(s2[i:]))[::-1]:
                        print(s2[:i-1]+s2[i:]+'a')
                        can=True
                        break
                                            
            if not can and s2[0]=='z':
                print(-1)
            elif not can:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        if list(s2[ans:])==sorted(s2[ans:])[::-1]:
            prob=False
            for j in range(25, -1, -1):
                if diff(s2[j-1],s2[j])>1:
                    J=j
                    prob=True
                    break
            if prob:
                temp=s2[J]
                t2=s2[J-1]
                s2=s2[:j-1]+temp+t2+s2[J+1:]
                print(s2)
            else:
                s2=s2[:ans]
                char=alp[alp.index(s2[ans-1])+1]
                s2=s2[:ans-1]+char
                print(s2)
        else:
            for j in range(25, -1, -1):
                if s2[j-1]<s2[j]:
                    J=j
                    break
            temp=s2[J]
            t2=s2[J-1]
            s2=s2[:j-1]+temp+t2+s2[J+1:]
            print(s2)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


a_code = 97
z_code = 122

def main(text):
    ret = text
    if text == 'zyxwvutsrqponmlkjihgfedcba':
        return -1
    l = len(text)
    ls = range(l)
    i = a_code
    while i <= z_code:
        flag = False
        for j in ls:
            if ord(text[j]) == i:
                break
        if j == l - 1 and ord(text[j]) != i: 
            break
        i = i + 1
    if i == 123:
        if text[0] == "a":
            abc = "abcdefghijklmnopqrstuvwxyz"
            for i in range(len(text)):
                if abc[i] == text[i]:
                    continue
                return abc[:i]+chr(ord(text[i-1])+1)
        else:
            abc = 'zyxwvutsrqponmlkjihgfedcba'
            for i in range(len(text)):
                if abc[i] == text[i]:
                    continue
                return abc[:i]+chr(ord(text[i])-1)
        return -1
    else:
        return ret + chr(i)

if __name__ == "__main__":
    s = raw_input()

    print(main(s))

Submission status: Wrong Answer

Solution code:
import sys

N = input()
a = []
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 = ['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']
l = list(N)
if N =='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
elif len(N) != 26:
  for i in range(len(N)):
    c.remove(l[i])
  N = N[0: i+1] + c.pop(0)
  print(N)
else:
  for i in range(len(N)-1,-1,-1):
    for j in range(i-1,-1,-1):
      if ord(l[i]) > ord(l[j]):
        N = N[0: j] + N[i]
        print(N)
        sys.exit()

Submission status: Wrong Answer

Solution code:
S = input()
Z = sorted(list(S))
G = ["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"]
ans = []

if "zz" in S:
    i = G.index(S[0])
    j = 1
    S = list(S)
    while j < 27:
        if G[(i + j) % 26] not in ans:
            S[0] = G[(i + j) % 26]
            break
        j += 1
    else:
        print(-1)

for s in S:
    if s not in ans:
        ans.append(s)
    else:
        i = G.index(s)
        j = 1
        while j < 27:
            if G[(i + j) % 26] not in ans:
                ans.append(G[(i + j) % 26])
                print("".join(ans))
                exit()
            j += 1

for i in range(100):
    aaa = sorted(ans)
    if "".join(ans) > S:
        print("".join(ans))
        exit()
    else:
        for g in G:
            if g not in ans:
                ans.append(g)
                break

ans = "".join(ans)
S = "".join(S)
if ans == S:
    ans = list(ans)
    aaa = sorted(list(ans))

    for g in G:
        if g not in ans:
            ans.append(g)
            break

    else:
        for i in range(len(S)-1, -1, -1):
            for j in range(i, -1, -1):
                if S[i] > S[j]:
                    print(S[:j] + S[i])
                    exit()
ans = "".join(ans)
if ans > S:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

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

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

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

appear = [0 for i in range(27)]
string = [0]
for i in range(n):
	string.append(d[s[i]])
	appear[d[s[i]]] += 1

exist = 0
for i in range(1,27):
	if appear[i] == 0:
		exist = i
		break

if exist != 0: #ない文字があった時
	string.append(exist)
	for k in range(1,n+2):
		if k != n+1:
			print(e[string[k]],end='')
		else:
			print(e[string[k]])

else: #全部ある時
	if s == 'zyxwvutsrqponmlkjihgfedcba':
		print('-1')
	else:
		bunki = 0
		bunki2 = []
		memo = []
		for i in range(1,26):
			if string[i] > string[i+1]:
				bunki = i
				bunki2.append(i)

		if bunki != 25:		
			temp = [0]
			temp[0] = string[25]
			string[25] = string[26]
			string[26] = temp[0]
			for h in range(1,27):
				if h != 26:
					print(e[string[h]],end='')
				else:
					print(e[string[h]])
			
		else:
			w = len(bunki2)
			ren = bunki2[w-1]
			while (ren-1) in bunki2:
				ren += -1
			string[ren-1] = d[s[25]]
			for h in range(1,ren):
				if h != ren-1:
					print(e[string[h]],end='')
				else:
					print(e[string[h]])

Submission status: Wrong Answer

Solution code:
import string
s = input()
s2=s.lower()
alp = string.ascii_lowercase
ans=''
def diff(a, b):
    if a=='a' or b=='a':
        if a=='z' or b=='z':
            return 1
    if a=='z' or b=='z':
        if a=='a' or b=='a':
            return 1
    return abs(alp.index(a)-alp.index(b))
if len(s)<26:
    for i in range(26):
        if alp[i] not in s2:
            ans=alp[i]
            break
    if ans=='':
        print(-1)
    else:
        print(s+ans)
        
else:
    found = False
    ans=-1
    for i in range(1,26):
        if diff(s2[i], s2[i-1])>1:
            found=True
            ans=i
            break

    if not found:
        if s2[0]<s2[1]:
            for j in range(25, -1, -1):
                if s2[j-1]<s2[j]:
                    J=j
                    break
            temp=s2[J]
            t2=s2[J-1]
            s2=s2[:j-1]+temp+t2+s2[J+1:]
            print(s2)
        else:
            can=False
            for i in range(1, 26):
                if s2[i]=='z' and s2[i-1]=='a':
                    if list(s2[:i])==sorted(list(s2[:i]))[::-1] and list(s2[i:])==sorted(list(s2[i:]))[::-1]:
                        print(s2[:i-1]+s2[i:]+'a')
                        can=True
                        break
                                            
            if not can and s2[0]=='z':
                print(-1)
            elif not can:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        for j in range(25, -1, -1):
            if s2[j-1]<s2[j]:
                J=j
                break
        temp=s2[J]
        t2=s2[J-1]
        s2=s2[:j-1]+temp+t2+s2[J+1:]
        print(s2)

Submission status: Wrong Answer

Solution code:
def solve(s):
  cnt="abcdefghijklmnopqrstuvwxyz"
  res=[0 for x in range(26)]
  for i in range(len(s)):
    a=cnt.index(s[i])
    res[a]+=1
  for i in range(26):
    if res[i]==0:
      return s+cnt[i]

def solve26(s):
  cnt="abcdefghijklmnopqrstuvwxyz"
  pos="zyxwvutsrqponmlkjihgfedcba"
  po=26
  for i in range(26):
    if s[i]!=pos[i]:
      po=i
      break
  z=s.index(pos[po])
  s=s[:z]
  res=[0 for x in range(26)]
  for i in range(len(s)):
    a=cnt.index(s[i])
    res[a]+=1
  y=cnt.index(s[-1])
  s=s[:-1]
  for i in range(y+1,26):
    if res[i]==0:
      return s+cnt[i]

S=input()
if len(S)==26 and S=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
elif len(S)==26:
  S=solve26(S)
  print(S)
else:
  S=solve(S)
  print(S)

Submission status: Wrong Answer

Solution code:
ab=[False]*26
vch={'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}
ch=raw_input().strip()
i="abcdefghijklmnopqrstuvwxyz"
if(ch==i or ch==i[::-1] or len(set(ch))!=len(ch)): print("%d"%(-1))
elif(len(ch)==26):
    for j in xrange(25,0,-1):
        if( abs(ord(ch[j])-ord(ch[j-1]))>1):
            idx=min(ord(ch[j]),ord(ch[j-1]))
            print("%s"%(ch[:j]+min(list(ch[j:]))))                           
else:
  for j in ch:
      ab[vch[j]]=True
  for j in xrange(26):
      if(ab[j]==False):
          break
  print("%s"%(ch+i[j]))