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 = list(input())
for i in range(len(s)):
    if s[i] == "a":
        s[i] = 0
    if s[i] == "b":
        s[i] = 1
    if s[i] == "c":
        s[i] = 2
    if s[i] == "d":
        s[i] = 3
    if s[i] == "e":
        s[i] = 4
    if s[i] == "f":
        s[i] = 5
    if s[i] == "g":
        s[i] = 6
    if s[i] == "h":
        s[i] = 7
    if s[i] == "i":
        s[i] = 8
    if s[i] == "j":
        s[i] = 9
    if s[i] == "k":
        s[i] = 10
    if s[i] == "l":
        s[i] = 11
    if s[i] == "m":
        s[i] = 12
    if s[i] == "n":
        s[i] = 13
    if s[i] == "o":
        s[i] = 14
    if s[i] == "p":
        s[i] = 15
    if s[i] == "q":
        s[i] = 16
    if s[i] == "r":
        s[i] = 17
    if s[i] == "s":
        s[i] = 18
    if s[i] == "t":
        s[i] = 19
    if s[i] == "u":
        s[i] = 20
    if s[i] == "v":
        s[i] = 21
    if s[i] == "w":
        s[i] = 22
    if s[i] == "x":
        s[i] = 23
    if s[i] == "y":
        s[i] = 24
    if s[i] == "z":
        s[i] = 25
if len(s) != 26:
    for i in range(26):
        if i not in s:
            s.append(i)
            break
    for i in range(len(s)):
        if s[i] == 0:
            s[i] = "a"
        if s[i] == 1:
            s[i] = "b"
        if s[i] == 2:
            s[i] = "c"
        if s[i] == 3:
            s[i] = "d"
        if s[i] == 4:
            s[i] = "e"
        if s[i] == 5:
            s[i] = "f"
        if s[i] == 6:
            s[i] = "g"
        if s[i] == 7:
            s[i] = "h"
        if s[i] == 8:
            s[i] = "i"
        if s[i] == 9:
            s[i] = "j"
        if s[i] == 10:
            s[i] = "k"
        if s[i] == 11:
            s[i] = "l"
        if s[i] == 12:
            s[i] = "m"
        if s[i] == 13:
            s[i] = "n"
        if s[i] == 14:
            s[i] = "o"
        if s[i] == 15:
            s[i] = "p"
        if s[i] == 16:
            s[i] = "q"
        if s[i] == 17:
            s[i] = "r"
        if s[i] == 18:
            s[i] = "s"
        if s[i] == 19:
            s[i] = "t"
        if s[i] == 20:
            s[i] = "u"
        if s[i] == 21:
            s[i] = "v"
        if s[i] == 22:
            s[i] = "w"
        if s[i] == 23:
            s[i] = "x"
        if s[i] == 24:
            s[i] = "y"
        if s[i] == 25:
            s[i] = "z"
    s_ = "".join(s)
    print(s_)
else:
    x = 1
    while True:
        if s[-x] > s[-x-1]:
            s1 = s[0:-x-1]
            s2 = s[-x-1] + 1
            s1.append(s2)
            break
        else:
            x = x + 1
            if x == 26:
                break
    if s == [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]:
        print("-1")
    else:
        for i in range(len(s1)):
            if s1[i] == 0:
                s1[i] = "a"
            if s1[i] == 1:
                s1[i] = "b"
            if s1[i] == 2:
                s1[i] = "c"
            if s1[i] == 3:
                s1[i] = "d"
            if s1[i] == 4:
                s1[i] = "e"
            if s1[i] == 5:
                s1[i] = "f"
            if s1[i] == 6:
                s1[i] = "g"
            if s1[i] == 7:
                s1[i] = "h"
            if s1[i] == 8:
                s1[i] = "i"
            if s1[i] == 9:
                s1[i] = "j"
            if s1[i] == 10:
                s1[i] = "k"
            if s1[i] == 11:
                s1[i] = "l"
            if s1[i] == 12:
                s1[i] = "m"
            if s1[i] == 13:
                s1[i] = "n"
            if s1[i] == 14:
                s1[i] = "o"
            if s1[i] == 15:
                s1[i] = "p"
            if s1[i] == 16:
                s1[i] = "q"
            if s1[i] == 17:
                s1[i] = "r"
            if s1[i] == 18:
                s1[i] = "s"
            if s1[i] == 19:
                s1[i] = "t"
            if s1[i] == 20:
                s1[i] = "u"
            if s1[i] == 21:
                s1[i] = "v"
            if s1[i] == 22:
                s1[i] = "w"
            if s1[i] == 23:
                s1[i] = "x"
            if s1[i] == 24:
                s1[i] = "y"
            if s1[i] == 25:
                s1[i] = "z"
        s2_ = "".join(s1)
        print(s2_)

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
        return "-1"
    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:
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] 
                break
            else:
                an = inp[:-2-i] + inp[-1]
                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:
A = input()
if A == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:

    isNoCheck = [True for _ in range(30)]

    for cha in A:
        isNoCheck[ord(cha) - ord('a')] = False

    isEnd = False
    num = 0
    while num <= ord('z') - ord('a'):
        if isNoCheck[num]:
            print(A+chr(num + ord('a')))
            isEnd = True
            break
        num = num + 1

    if isEnd:
        pass
    else:
        isEnd = False
        num2 = len(A) - 1
        min = 'z'
        while num2 >= 0:
            num = num2 - 1
            if min >= A[num2]:
                min = A[num2]

            while num >= 0:
                if A[num2] > A[num]:

                    print(A[:num] + min)
                    isEnd = True
                    break
                num = num - 1

            if isEnd:
                break
            num2 = num2 - 1

#zyxwvutsrqponmlkjihgfedcba
#srqponmzyxwvutlkjihgfedcba

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]
        cc = c
        xx = f(s)
        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:
            del s[-1]
            xx = f(s)
            s[-1] = sorted(list(xx))[0]
            s = ''.join(s)
            print(s)

Submission status: Wrong Answer

Solution code:
import sys


alph="abcdefghijklmnopqrstuvwxyz"
allis=list(alph)
S=input()
if(S=="zyxwvutsrqponmlkjihgfedcba"):
    print(-1)
    sys.exit()

"""
if(S=="abcdefghijklmnopqrstuvwxyz"):
    print("b")
    sys.exit()
"""
for i in alph:
    if(i not in S):
        print(S+i)
        sys.exit()
        
"""
outp=""
for i in range(len(S)):
    if(S[i]==alph[i]):
        outp+=S[i]
    else:
        outp=outp[:-1]
        outp=outp+alph[i]
        break
"""
for i in range(len(S)-1)[::-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
                print(S[:i]+j)
                sys.exit()
    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
                print(S[:i+1]+j)
                sys.exit()
print("b")

Submission status: Wrong Answer

Solution code:
import string


def main():
    """
    abcdefghijklmnopqrstuvwxyz
    zyxwvutsrqponmlkjihgfedcba
    """
    S = input()

    # 全文字使ってsort済み
    az = string.ascii_lowercase
    za = az[::-1]
    if S == az or S == za:
        print(-1)
        return

    if len(S) < len(az):
        # 全文字使ってない
        s_s = set(S)
        s_az = set(az)
        a = sorted(list(s_az - s_s))[0]

        # S=abのとき、abc < ac==True なので追加で良い
        ans = S + a
        print(ans)
        return

    for i in range(1, len(S) + 1):
        # sort済みかつ連続であるか
        # if S[:i] == az[:i]:
        if S[:i] == az[:i] or S[:i] == za[:i]:
            continue

        rev = 1
        if S[:i-1] == az[:i-1]:
            rev = 0
        # sortずみでないので、sort済みまでの最後を置き換え
        if S[i:]:
            a = sorted(S[i:])[0]
            ans = S[:i-2+rev] + a
            print(ans)
            return
        else:
            print(-1)
            return

if __name__ == '__main__':
    main()

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(25,0,-1):
        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:
        pos1=''
        pos2=''
        if s2[ans:] in alp[alp.index(s2[ans-1]):][::-1]:
            s2=s2[:ans+1]
            char=alp[alp.index(s2[ans-1])+1]
            s2=s2[:ans-1]+char
            pos1=s2
            s2=s
            s2=s2[:ans]
            char=alp[alp.index(s2[ans-1])+1]
            s2=s2[:ans]+char
            pos2=s2
            ans=min(pos1, pos2)
            print(ans)
        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:
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]
        cc = c
        xx = f(s)
        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:
            s[-1] = sorted(list(xx))[0]
            s = ''.join(s)
            print(s)

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 text[:-1]
        return -1
    else:
        return ret + chr(i)

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

    print(main(s))

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) and (s[i] == hoge[0]):
                    hoge = hoge[1:]
                    result += s[i]
                else:
                    if len(result) == 1:
                        print(result + hoge[0])
                    else:
                        print(result[:-1] + 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:
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] 
                break
            else:
                an = inp[:-2-i] + inp[-1-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 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:])==alp[alp.index(s2[ans]):][::-1]:
            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 -*-

# 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) and (s[i] == hoge[0]):
                    hoge = hoge[1:]
                    result += s[i]
                else:
                    print(result[:-1] + 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:
        print(ans)
        if s2[ans:] in alp[alp.index(s2[ans-1]):][::-1]:
            s2=s2[:ans+1]
            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:
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:
            s[-1] = sorted(list(xx))[0]
            s = ''.join(s)
            print(s)

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(25,0,-1):
        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]
            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:
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 == '':
        print(-1)
    else:
        for i in range(len(S)):
            s_lis2.remove(S[i])

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

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]
                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:
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]): print("%d"%(-1))
elif(len(ch)==26):
    test=False
    for j in xrange(25,0,-1):
        if(abs(ord(ch[j])-ord(ch[j-1]))>1):
            print("%s"%(ch[:j-1]+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(25,0,-1):
        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]
            char=alp[alp.index(s2[ans-1])+1]
            s2=s2[: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:
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':
        if s == 'zyxwvutsrqponmlkjihgfedcba':
            print(-1)
        else:
            temp = ''
            for i, t in enumerate(dic_copy[::-1]):
                if s[i] != t:
                    temp = temp + dic_copy[dic_copy.index(s[i])+1]
                    break
                else :
                    temp = temp + s[i]
            print(temp)
    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:
def agc022A():
    from collections import OrderedDict
    S = input()
    alfa_list = [chr(i) for i in range(97, 97 + 26)]
    alfa_dict = OrderedDict()

    # {"a":0, ...}
    for i in range(26):
        alfa_dict[alfa_list[i]] = i

    S_max = "zyxwvutsrqponmlkjihgfedcba"
    S_min = "abcdefghijklmnopqrstuvwxyz"

    if S == S_max:
        print(-1)

    elif len(S) < 26:
        for i in range(len(S)):
            del alfa_dict[S[i]]

        remainings = [k for k, v in alfa_dict.items()]
        out = S + remainings[0]
        print(out)
        return 0

    else:
        alfa_dict2 = alfa_dict.copy()
        for i in range(26):
            tmp = alfa_dict[S[i]]
            del alfa_dict[S[i]]

            if S[i] != S_min[i]:
                remainings = [k for k, v in alfa_dict.items() if v > alfa_dict2[S[i - 1]]]

                if S[i:] == S_max[:26 - i]:
                    out = S[:i - 1] + remainings[0]
                    print(out)
                    return 0
                else:
                    out = S[:i + 1] + remainings[1]
                    print(out)
                    return 0
        print(S_min)
        return 0

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:
            s[-1] = c
            s = ''.join(s)
            print(s)