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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = input()
l = len(S)
colors = [0]*26
for s in S:
    colors[ ord(s) - ord('a')] = 1
check = 1
if not l == 26:
    for i in range(26):
        if colors[i] == 0:
            ans = S + chr(i+ord('a'))
            break
else:
    check = 0
    now = ord("a")-1
    for i in range(25,-1,-1):
        if ord(S[i]) < now:
            ans = S[:i]+S[-1]
            check = 1
            break
        else:
            now = ord(S[i])
if check:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
        print("-1")
        exit()
S = list(S)
S = [ord(e) for e in S]
ord_list = list(range(97,123))
# アルファベットが26個含まれているかチェック
for e in ord_list:
        if not e in S:
                S.append(e)
                S = [chr(e) for e in S]
                print("".join(S))
                exit()
#以下はアルファベット26個確定　後ろから最適になるように文字を置換していく
for i in range(len(S)-2,-1,-1):
        note = 123
        for j in range(i+1,len(S)-1):
                if S[i] < S[j] and S[j] < note:
                        note = S[j]
        if note < 123:
                S[i] = note
                del S[i+1:]
                S = [chr(e) for e in S]
                print("".join(S))
                break

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
        print("-1")
        exit()
S = list(S)
S = [ord(e) for e in S]
ord_list = list(range(97,123))
# アルファベットが26個含まれているかチェック
for e in ord_list:
        if not e in S:
                S.append(e)
                S = [chr(e) for e in S]
                print("".join(S))
                exit()
#以下はアルファベット26個確定　後ろから最適になるように文字を置換していく
for i in range(len(S)-1,0,-1):
        for j in range(i-1,-1,-1):
                if S[j] < S[i]:
                        S[j] = S[i]
                        del S[j+1:]
                        S = [chr(e) for e in S]
                        print("".join(S))
                        break
        else:
                continue
        break

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
        print("-1")
        exit()
S = list(S)
S = [ord(e) for e in S]
ord_list = list(range(97,123))
# アルファベットが26個含まれているかチェック
for e in ord_list:
        if not e in S:
                S.append(e)
                S = [chr(e) for e in S]
                print("".join(S))
                exit()
#以下はアルファベット26個確定　後ろから最適になるように文字を置換していく
for i in range(len(S)-2,-1,-1):
        if S[i] < S[-1]:
                S[i] = S[-1]
                del S[i+1:]
                break
S = [chr(e) for e in S]
print("".join(S))

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print("-1")
    exit()
S = list(S)
S = [ord(e) for e in S]
ord_list = list(range(97,123))
# アルファベットが26個含まれているかチェック
for e in ord_list:
    if not e in S:
        S.append(e)
        S = [chr(e) for e in S]
        print("".join(S))
        exit()
#以下はアルファベット26個確定　後ろから最適になるように文字を置換していく
for i in range(len(S)-2,-1,-1):
    S_part = S[i+1:]
    if S[i] < min(S_part):
        S[i] = min(S_part)
        del S[i+1:]
        break
S = [chr(e) for e in S]
print("".join(S))

Submission status: Wrong Answer

Solution code:
S = list(input())
if "".join(S) == "zyxwvutsrqponmlkjihgfedcba":
    print("-1")
    exit()
elif "".join(S) == "abcdefghijklmnopqrstuvwxyz":
    print("abcdefghijklmnopqrstuvwxz")
    exit()
S = [ord(e) for e in S]
ord_list = list(range(97,123))
for e in ord_list:
    if not e in S:
        S.append(e)
        S = [chr(e) for e in S]
        print("".join(S))
        exit()
for i in range(len(S)-2,-1,-1):
    if  abs(S[i+1] - S[i]) != 1:
        note = i
# S_part は1刻みの単調なリスト
if S[-2] < S[-1]:
    S[-2] = S[-1]
    del S[-1]
else:
    for e in S[note + 1:]:
        if e == S[note] + 1:
            S[note] = e
            del S[note + 1:]
S = [chr(e) for e in S]
ans = "".join(S)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
s = input()
x = list("abcdefghijklmnopqrstuvwxyz")

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

Submission status: Wrong Answer

Solution code:
S=input()
T="zyxwvutsrqponmlkjihgfedcba"
X=list(T)
Y=[]
tmp=[0,""]

for i in range(len(S)):
    if S[i] == "z":
        tmp[0] = i
        tmp[1] = min(X)
    Y.append(S[i])
    X.remove(S[i])

if "".join(Y) == T:
    print(-1)
elif len(X) != 0:
    Y.append(min(X))
    print("".join(Y))
else:
    Y[tmp[0]-1] = tmp[1]
    print("".join(Y[:tmp[0]]))

Submission status: Wrong Answer

Solution code:
S=input()
T="zyxwvutsrqponmlkjihgfedcba"
X=list(T)
Y=[]
tmp=[0,""]

for i in range(len(S)):
    if S[i] == "z":
        tmp[0] = i - 1
        tmp[1] = min(X)
    Y.append(S[i])
    X.remove(S[i])

if "".join(Y) == T:
    print(-1)
elif len(X) != 0:
    Y.append(min(X))
    print("".join(Y))
else:
    Y[tmp[0]] = tmp[1]
    print("".join(Y[:tmp[0]]))

Submission status: Wrong Answer

Solution code:

S=input()
X=list("abcdefghijklmnopqrstuvwxyz")
Y=[]
tmp=0

for i in range(len(S)):
    if S[i] == "z":
        tmp = i - 1
        break
    Y.append(S[i])
    X.remove(S[i])
    tmp+=1

if tmp == -1:
    print(-1)
else:
    if len(Y) <= tmp:
        Y.append(min(X))
    else:
        Y[tmp] = min(X)
    
print("".join(Y))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
s = input()
x = list("abcdefghijklmnopqrstuvwxyz")

if len(s)<26:
    for c in x:
        if c not in s:
            print(s+c)
            exit()
else:
    s = list(s)
    for i in range(len(s)-2,-1,-1):
        if s[i]<s[-1]:
            s[i],s[-1] = s[-1],s[i]
            print("".join(s[:i+1]))
            exit()
print(-1)

Submission status: Wrong Answer

Solution code:
S = input()

abc = 'abcdefghijklmnopqrstuvwxyz'

less_abc = abc

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

ans = ''
if len(S) != 26:
    ans = S + less_abc[0]
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    long_now = ''
    long_s = ''
    num = 0
    ind = 27
    for s in S:
        ind_now = abc.index(s)
        if ind > ind_now:
            ind = ind_now
            long_now += s
        else:
            if len(long_s) <= len(long_now):
                long_s = long_now

            long_now = s
            ind = ind_now

    if len(long_s) <= len(long_now):
        long_s = long_now

    ind = S.index(long_s)
    S = S[:ind]
    for s in abc[abc.index(S[-1])+1:]:
        if s in long_s:
            ans = S + s
            break

print(ans)

Submission status: Wrong Answer

Solution code:
A = []
for i in range(ord("a"),ord("z")+1) :
    a = chr(i)
    A.append(a)

S = list(input())

p = ""
for i in A :
    if i not in S :
        p = i
        break
ans = "".join(S)+p
if ans != "".join(S) :
    print(ans)
else :
    if ans == "zyxwvutsrqponmlkjihgfedcba" :
        print(-1)
    else :
        
        ANS = []
        for i in range(len(ans)) :
            if ans[i] != "z" :
                ANS.append(ans[i])
            else :
                ANS[-1] = ans[-1]
                break
        print("".join(ANS))

Submission status: Wrong Answer

Solution code:
A = []
for i in range(ord("a"),ord("z")+1) :
    a = chr(i)
    A.append(a)

S = list(input())

p = ""
for i in A :
    if i not in S :
        p = i
        break
ans = "".join(S)+p
if ans != "".join(S) :
    print(ans)
else :
    if ans == "zyxwvutsrqponmlkjihgfedcba" :
        print(-1)
    else :
        
        ANS = []
        ANS.append(ans[0])
        for i in range(len(ans)) :
            if ans[i] != "z" :
                ANS.append(ans[i])
            else :
                ANS[-1] = ans[-1]
                break
        print("".join(ANS))

Submission status: Wrong Answer

Solution code:
s=input()
al=[chr(ord('a') + i) for i in range(26)]
al=[i for i in al if i not in s]
if not al:
    print(-1)
    exit()
for i,j in enumerate(s):
    if al[0]<j:
        print(s[:i]+al[0]+s[i:])
        break

Submission status: Wrong Answer

Solution code:
import sys

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

    
S = sys.stdin.readline().strip()

c_list = [chr(i) for i in range(97, 97+26)]
#num_list = [int(chr(s)) for s in S]
s_list = [s for s in S]
all_char = "abcdefghijklmnopqrstuvwxyz"

if S == "zyxwvutsrqponmlkjihgfedcba":
    print("-1")
elif len(S) != 26:
    for c in c_list:
        if c not in s_list:
            print(S + c)
            break
else:
    for i in reversed(list(range(26))):
        sub = S[i+1:]
        if forward(sub, S[i]):
            # S[i]が最大
            pass
        else:
            m = max(sub)
            tmp = []
            for mm in sub:
                if mm != m:
                    tmp.append(mm)
            tmp.append(S[i])
            print(S[:i] + m)
            break

Submission status: Wrong Answer

Solution code:
import sys

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

    
S = sys.stdin.readline().strip()

c_list = [chr(i) for i in range(97, 97+26)]
#num_list = [int(chr(s)) for s in S]
s_list = [s for s in S]
all_char = "abcdefghijklmnopqrstuvwxyz"

if S == "zyxwvutsrqponmlkjihgfedcba":
    print("-1")
elif len(S) != 26:
    for c in c_list:
        if c not in s_list:
            print(S + c)
            break
else:
    for i in reversed(list(range(26))):
        sub = S[i+1:]
        if forward(sub, S[i]):
            # S[i]が最大
            pass
        else:
            m = max(sub)
            tmp = []
            for mm in sub:
                if mm != m:
                    tmp.append(mm)
            tmp.append(S[i])
            print(S[:i] + m + "".join(sorted(tmp)))
            break

Submission status: Wrong Answer

Solution code:
alphabet = list("abcdefghijklmnopqrstuvwxyz")
S = raw_input()
ans = None
if len(S) < 26:
  for ab in alphabet:
    if not (ab in S):
      ans = S +ab
      break
else:
  if S == "zyxwvutsrqponmlkjihgfedcba":
    ans = -1
  else:
    for i in range(len(S)-1):
      if S[i] > S[i+1]:
        break
      ans = S[:i]+alphabet[alphabet.index(S[i])+1]
print ans

Submission status: Wrong Answer

Solution code:
alphabet = list("abcdefghjijklmnopqurstuvwxyz")
S = raw_input()
ans = None
if len(S) < 26:
  for ab in alphabet:
    if not (ab in S):
      ans = S +ab
      break
else:
  if S == "zyxwvutsrqponmlkjihgfedcba":
    ans = -1
  else:
    for i in range(len(S)-1):
      if S[i] > S[i+1]:
        break
      ans = S[:i]+alphabet[alphabet.index(S[i])+1]
print ans

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
for i in range(97, 97+26):
    if chr(i) not in s:
        print(s + chr(i))
        exit()

for i in range(24, -1, -1):
    if s[i] > s[i+1]:
        continue
    else:
        temp = s[:i] + min(sorted(s[i+1:]))
        print(temp)
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
if len(s) != 26:
    for i in range(97, 97+26):
        if chr(i) not in s:
            print(s + chr(i))
            exit()

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

for i in range(26):
    temp = sorted(s[i:])
    if s[i] == temp[0]:
        continue
    else:
        print(s[:i] + temp[0])
        exit()
print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
if len(s) != 26:
    for i in range(97, 97+26):
        if chr(i) not in s:
            print(s + chr(i))
            exit()
elif s != "zyxwvutsrqponmlkjihgfedcba":
    for i in range(97, 97+26):
        if s[i-97] != chr(i):
            t = sorted(list(s[i-97:])).pop(0)
            print(s[:i-97]+t)
            exit()
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s=input()
x=[0 for _ in range(26)]
if len(s)<26:
    for i in range(len(s)):
        x[ord(s[i])-ord('a')]=1
    for j in range(26):
        if x[j]==0:
            k=j
            break
    print("".join([s,chr(ord('a')+k)]))
elif s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    mi=-1
    for i in range(25):
        if s[i]>s[i+1]:
            x[i]=1
            if mi==-1:
                mi=i
        elif mi!=-1:
            mi=-1
    tx=s[mi::]
    mx=min(list(tx))
    print(s[:mi-1]+mx)

Submission status: Wrong Answer

Solution code:
s=input()
x=[0 for _ in range(26)]
if len(s)<26:
    for i in range(len(s)):
        x[ord(s[i])-ord('a')]=1
    for j in range(26):
        if x[j]==0:
            k=j
            break
    print("".join([s,chr(ord('a')+k)]))
elif s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    mi=-1
    for i in range(25):
        if s[i]>s[i+1]:
            x[i]=1
            if mi==-1:
                mi=i
    tx=s[mi::]
    mx=min(list(tx))
    print(s[:mi-1]+mx)

Submission status: Wrong Answer

Solution code:
s=input()
x=[0 for _ in range(26)]
if len(s)<26:
    for i in range(len(s)):
        x[ord(s[i])-ord('a')]=1
    for j in range(26):
        if x[j]==0:
            k=j
            break
    print("".join([s,chr(ord('a')+k)]))
#elif s=="zyxwvutsrqponmlkjihgfedcba":
#    print(-1)
else:
    mi=-1
    for i in range(25):
        if s[i]>s[i+1]:
            x[i]=1
            if mi==-1:
                mi=i
    tx=s[mi::]
    mx=min(list(tx))
    print(s[:mi-1]+mx)

Submission status: Wrong Answer

Solution code:
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]
    for i in range(26):

        if alphabet_to_int(prev_char)>alphabet_to_int(S[-(i+1)]):
            ans=S[:-(i+1)]+prev_char    
            print ans
            break
        if i==25:
            print -1

Submission status: Wrong Answer

Solution code:
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]
    for i in range(26):

        if alphabet_to_int(prev_char)>alphabet_to_int(S[-(i+1)]):
            ans=S[:-(i+1)]+prev_char    
            print ans
            break
        if i==25:
            print -1
        prev_char=S[-(i+1)]

Submission status: Wrong Answer

Solution code:

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_set=[0 for i in range(26)]
    prev_set[alphabet_to_int(S[-1])]=1
    prev_char=S[-1]
    for i in range(26):
        prev_set[alphabet_to_int(S[-(i+1)])]=1
        if alphabet_to_int(prev_char)>alphabet_to_int(S[-(i+1)]):
            ans=S[:-(i+1)]+prev_char
            prev_set[alphabet_to_int(prev_char)]=0
            for j in range(26):
                if prev_set[j]==1:
                    ans+=int_to_alphabet(j)      
            print ans
            break
        if i==25:
            print -1
        prev_char=S[-(i+1)]

Submission status: Wrong Answer

Solution code:
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_set=[0 for i in range(26)]
    prev_set[alphabet_to_int(S[-1])]=1
    prev_char=S[-1]
    for i in range(26):
        prev_set[alphabet_to_int(S[-(i+1)])]=1
        if alphabet_to_int(prev_char)>alphabet_to_int(S[-(i+1)]):
            ans=S[:-(i+1)]+prev_char
            prev_set[alphabet_to_int(prev_char)]=0
            for j in range(26):
                if prev_set[j]==1:
                    ans+=int_to_alphabet(j)      
            print ans
            break
        if i==25:
            print -1

Submission status: Wrong Answer

Solution code:

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_set=[0 for i in range(26)]
    prev_set[alphabet_to_int(S[-1])]=1
    prev_char=S[-1]
    for i in range(26):
        prev_set[alphabet_to_int(S[-i])]=1
        if alphabet_to_int(prev_char)>alphabet_to_int(S[-i]):
            ans=S[:-i]+prev_char
            prev_set[alphabet_to_int(prev_char)]=0
            for j in range(26):
                if prev_set[j]==1:
                    ans+=int_to_alphabet(j)      
            print ans
            break
        if i==25:
            print -1