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: Runtime Error

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]:
            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: Runtime Error

Solution code:
#problem A

S = input()

def find_next_letter(S):
    letters = "abcdefghijklmnopqrstuvwxyz"
    letters_dict = {}
    for letter in letters:
        letters_dict[letter] = 0

    for letter in S:
        letters_dict[letter] += 1

    for key, value in letters_dict.items():
        if value == 0:
            return key
            break
        else:
            pass

def main():
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        print(S + find_next_letter(S))

main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import functools
import math
from itertools import combinations
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    ham = combinations(range(1, 30001), 100)
    for x in ham:
        if functools.reduce(math.gcd, x) == 1:
            for y in x:
                xx = list(x)
                xx.remove(y)
                if functools.reduce(math.gcd, [y, sum(xx)]) != 1:
                    break
            else:
                print(' '.join([str(i) for i in x]))
                return


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import string
alphabets = string.ascii_lowercase

def alphabet_check(S, checker):
    for i, s in enumerate(S):
        checker[alphabets.index(s)] = i

S = input()
checker = [-1]*26

alphabet_check(S, checker)

if len(S) < 26:
    char_idx = checker.index(-1) 
    print(S + alphabets[char_idx])
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    cont_flag = False
    for i in range(26-1):
        if S[i] > S[i+1]:
            if cont_flag == False:
                idx = i
                cont_flag = True
        else:
            cont_flag = False
            
    checker2 = [-1]*26
    if idx == 0:
        st = ''
        for i in range(26):
            if alphabets[i] > S[idx]:
                char_idx = i
                break
    else:
        st = S[:idx-1]
        alphabet_check(st, checker2)
        for i in range(26):
            if checker2[i] == -1 and alphabets[i] > S[idx-1]:
                char_idx = i
                break
    print(st + alphabets[char_idx])

Submission status: Runtime Error

Solution code:
# encoding:utf-8
 
import fractions
 
def check(l):
	l = list(map(int, l.split()))
 
	if max(l) >= 30001:
		return False
	tmp = l[0]
 
	for i in range(len(l) - 1):
		tmp = fractions.gcd(tmp, l[i+1])
	if tmp != 1:
		return False 
 
	for i in range(len(l)):
		if fractions.gcd(l[i], sum(l) - l[i]) == 1:
			return False
 
	return True
 
 
def main(n):
	if n == 3:
		return "2 5 63"
	if n <= 5003:
		sub = [i * 3 for i in range(1, 10001) if i % 2 == 1]
		if n % 2 == 0:
			s = "2 4 " + " ".join([str(x) for x in sub[0:(n-2)]])
			return(s)
		else:
			s = "2 4 6 " + " ".join([str(x) for x in sub[0:(n-3)]])
			return(s)
	else:
		sub = [i * 3 for i in range(1, 10001) if i % 2 == 1]
		if n % 2 == 0:
			s = " ".join([str(i) for i in range(1, 30000) if i % 2 == 0 and i % 3 != 0][0:(n-5000)]) + " " +  " ".join([str(x) for x in sub])
			return(s)
		else:
			s = " ".join([str(i) for i in range(1, 30000) if i % 2 == 0 and i % 3 != 0][0:(n-5001)]) + " 6 " +  " ".join([str(x) for x in sub])
			return(s)
if __name__ == '__main__':
	n = int(input())
	print(main(n))
	#for i in range(5001, 30000):
	#	print(i, check(main(i)))

Submission status: Runtime Error

Solution code:
s = input()

ax = set()

a = 'zyxwvutsrqponmlkjihgfedcba'

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:
        s = list(s)
        c = s[-1]
        while ord(s[-1]) >= ord(c):
            del s[-1]
        s[-1] = c
        print(''.join(s))
    else:
        print(g(s, xx))

Submission status: Runtime Error

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:
            if s2[0]=='z':
                print(-1)
            else:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        if list(s2[ans:])==sorted(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: Runtime Error

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:
            if s2[0]=='z':
                print(-1)
            else:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        if list(s2[ans:])==sorted(s2[ans:])[::-1]:
            s2=s2[:ans]
            char=alp[alp.index(s2[ans-1])+1]
            s2=s2[:ans-1]+char
            if list(set(list(s2)))!= sorted(list(s2)):
                char=s2[0]
                if char=='z':
                    print(-1)
                else:
                    print(alp[alp.index(char)+1])
            else:
                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: Runtime Error

Solution code:
s = input()

ax = set()

a = 'zyxwvutsrqponmlkjihgfedcba'

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]

xx = f(s)

if len(xx) == 0:
    s = list(s)
    c = s[-1]
    while ord(s[-1]) >= ord(c) - 1:
        del s[-1]
    s += c
    print(''.join(s))
else:
    print(g(s, xx))

Submission status: Runtime Error

Solution code:
import string
alphabets = string.ascii_lowercase

def alphabet_check(S, checker):
    for i, s in enumerate(S):
        checker[alphabets.index(s)] = i

S = input()
checker = [-1]*26

alphabet_check(S, checker)

if len(S) < 26:
    char_idx = checker.index(-1) 
    print(S + alphabets[char_idx])
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    cont_flag = False
    for i in range(26-1):
        if S[i] > S[i+1]:
            if cont_flag == False:
                idx = i
                cont_flag = True
        else:
            cont_flag = False
    string = S[:idx-1]
    checker2 = [-1]*26
    alphabet_check(string, checker2)
    for i in range(26):
        if checker2[i] == -1 and alphabets[i] > S[idx-1]:
            char_idx = i
            break
    print(string + alphabets[char_idx])

Submission status: Runtime Error

Solution code:
s= input()
alph="abcdefghijklmnopqrstuvwxyz"
if len(s)<26:
    alphplist=[a for a in alph if not (a in s)]
    ss=s+alphplist[0]
elif s==alph:
    ss="-1"
else :
    for i in range(26):
        if s[i] != alph[i]:
            ssp=s[0:i]
            ssp2=s[i:26]
            alphp=alph[i:26]
            alphprev=alphp[::-1]
            if alphprev==ssp2:
                ss=s[0:i-1]+alph[i]
            else:
                for j in range(len(ssp)):
                    if ssp2[j] != alphprev[j]:
                        ss=ssp+ssp2[0:j]+alph[alph.find(ssp2[j])+1]
                        break
            break
print(ss)

Submission status: Runtime Error

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:
            if s2[0]=='z':
                print(-1)
            else:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        if list(s2[ans:])==sorted(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: Runtime Error

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"
        else:
            abc = 'zyxwvutsrqponmlkjihgfedcba'
        for i in range(len(text)):
            if abc[i] == text[i]:
                continue
            return abc[:i-1]+abc[i]
        return -1
    else:
        return ret + chr(i)

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

    print(main(s))

Submission status: Runtime Error

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

plot = []
S = list(S)
if len(S) < 26:
    for s in S:
        if s != Alph[25]:
            plot.append(Alph[Alph.index(s)+1])
        else:
            continue

    plot.sort()
    for i in range(len(plot)):
        if plot[i] in S:
            plot[i] = '0'

    plot = list(set(plot))
    plot.remove('0')
    plot.sort()
    S.append(plot[0])
    print(''.join(S))

else:
    if ''.join(S) == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        for i in range(len(S)):
            if S[i] == Alph[i]:
                continue
            else:
                del S[i+1:]
                S.append(Alph[i])
                print(''.join(S))
                break
            print(-1)

Submission status: Runtime Error

Solution code:
S = list(input())

P = set(S)

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

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

Submission status: Runtime Error

Solution code:

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 == {}:
        #全部使われる場合
        for i in range(len(Slist)):
            if Slist[i] == Alpha[i]:
                ANS.append(Slist[i])
            else:
               ANS.pop()
               ANS.append(Alpha[i])
               for x in ANS:
                   ANSm += x 
               break
        print(ANSm)
        
    else:
        minS = min([(v,k) for k,v in alpha_dict.items()])[1]
        print(S+minS)

Submission status: Runtime Error

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:
            common_alphabet = ''

            for i in range(len(s)):
                if s[:i] == alphabets[:i]:
                    common_alphabet = s[:i]
            new_s = alphabets.index(''.join(common_alphabet[-2:-1]))
            print(''.join(common_alphabet[:-1]) + alphabets[new_s + 2])
    else:
        sorted_s = sorted(s)
        diff = set(alphabets) - set(sorted_s)
        candidate = sorted(diff)[0]
        print(''.join(s + [candidate]))

Submission status: Runtime Error

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")
if 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 == {}:
        #全部使われる場合
        for i in range(len(Slist)):
            if Slist[i] == Alpha[i]:
                ANS.append(Slist[i])
            else:
               ANS.pop()
               ANS.append(Alpha[i])
               for x in ANS:
                   ANSm += x 
               break
        print(ANSm)
        
    else:
        minS = min([(v,k) for k,v in alpha_dict.items()])[1]
        print(S+minS)

Submission status: Runtime Error

Solution code:
import string
alphabets = string.ascii_lowercase

def alphabet_check(S, checker):
    for i, s in enumerate(S):
        checker[alphabets.index(s)] = i

S = input()
checker = [-1]*26

alphabet_check(S, checker)

if len(S) < 26:
    char_idx = checker.index(-1) 
    print(S + alphabets[char_idx])
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    cont_flag = False
    for i in range(len(S)-1):
        if S[i] > S[i+1]:
            if cont_flag == False:
                idx = i
                cont_flag = True
    string = S[:idx]
    l = len(string)
    checker = [-1]*26
    alphabet_check(string, checker)
    char_idx = checker.index(-1)
    print(string[:l-1] + alphabets[char_idx])

Submission status: Runtime Error

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

alp = "abcdefghijklmnopqrstuvwxyz"

s = list(input())
ss = set(s)

if len(s)<26:
    for x in list(alp):
        if x not in ss:
            print("".join(s)+x)
            exit()

idx = 0
for (i,x) in enumerate(s):
    if i>1 and s[i-2]<s[i-1] and s[i-1]>s[i]:
        y = min(list(filter(lambda x:x>s[i-2], s[i-1:])))
        print("".join(s[0:i-2])+y)
        exit()
    elif i>1 and s[i-2]>s[i-1] and s[i-1]<s[i]:
        y = min(list(filter(lambda x:x>s[i], s[i-1:])))
        print("".join(s[0:i])+y)
        exit()


print("-1")

Submission status: Runtime Error

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(s2[i])-alp.index(s2[i-1]))
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:
            if s2[0]=='z':
                print(-1)
            else:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        if list(s2[ans:])==sorted(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: Runtime Error

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(s2[i])-alp.index(s2[i-1]))
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=s[J]
            t2=s[J-1]
            s=s[:j-1]+temp+t2+s[J+1:]
            print(s)
        else:
            if s2[0]=='z':
                print(-1)
            else:
                s=alp[alp.index(s1[0])+1]
                print(s2)
    else:
        if list(s2[ans:])==sorted(s2[ans:])[::-1]:
            s=s[:ans]
            char=alp[alp.index(s[ans-1])+1]
            s=s2[:ans-1]+char
            print(s2)
        else:
            for j in range(25, -1, -1):
                if s2[j-1]<s2[j]:
                    J=j
                    break
            temp=s[J]
            t2=s[J-1]
            s2=s[:j-1]+temp+t2+s[J+1:]
            print(s)

Submission status: Runtime Error

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(s2[i])-alp.index(s2[i-1]))
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:
            if s2[0]=='z':
                print(-1)
            else:
                s2=alp[alp.index(s2[0])+1]
                print(s2)
    else:
        if list(s2[ans:])==sorted(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: Runtime Error

Solution code:
S = list(input())

P = set(S)

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

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

Submission status: Runtime Error

Solution code:


def main():
    temp=input()
    t1=""
    t2=""
    # if temp=="zyxwvutsrqponmlkjihgfedcba":
    #     print(-1)
    #     exit()
    
    # elif "z" in temp and len(temp)==26:
    #     t1,t2=temp.split("z")
    #     temp=t1[:-1]
    #     temp+=sorted(list(t2))[0]
    #     print(temp)
    #     exit()       
    # else:
    alps=[chr(ord('a') + i) for i in range(26)]
    for w in list(temp):
        alps.remove(w)
    temp=temp+alps[0]
    print(temp)
    exit()       

    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:


def main():
    temp=input()
    t1=""
    t2=""
    if temp=="zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        exit()
    
    elif "z" in temp and len(temp)==26:
        t1,t2=temp.split("z")
        temp=t1[:-1]
        temp+=sorted(list(t2))[0]
        print(temp)
        exit()       
    else:
        alps=[chr(ord('a') + i) for i in range(26)]
        for w in list(temp):
            alps.remove(w)
        temp=temp+alps[0]
        print(temp)
        exit()       

    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import string
S = input()
if S == string.ascii_lowercase[::-1]:
    print(-1)
elif len(S) == 26:
    for i in range(len(S)-1):
        if all([S[i] > S[i+j] for j in range(1,len(S)-i)]):
            ans = S[:i]+min(S[i:])
            break
        elif any([S[i] > S[i+j] for j in range(1,len(S)-i)]):
            ans = S[:i] + min(filter(lambda x:x>S[i],S[i:]))
            break
    print(ans)
else:
    po = ""
    for char in string.ascii_lowercase:
        if char not in S:
            po = char
            break
        else:
            pass
    print(S+po)

Submission status: Runtime Error

Solution code:
import sys
s = input()
ss = list(s)

if len(ss) > 26:
    print(-1)
else:
    ngchar = ''
    while len(ss) > 0:
        res = -1
        for i in range(97, 122):
            if chr(i) not in ss:
                if chr(i) != ngchar:
                    print(''.join(ss) + chr(i))
                    sys.exit(1)
        if res == -1:
            ngchar = ss.pop(-1)
        else:
            break

Submission status: Runtime Error

Solution code:
import sys
s = input()
ss = list(s)

if len(ss) > 26:
    print(-1)
else:
    ngchar = ''
    while len(ss) > 0:
        res = -1
        for i in range(97, 122):
            if chr(i) not in ss:
                if chr(i) != ngchar:
                    print(''.join(ss) + chr(i))
                    sys.exit(1)
        if res == -1:
            ngchar = ss.pop(-1)
        else:
            break

Submission status: Runtime Error

Solution code:
from collections import Counter
import string
S = input()
if len(S) == 26 and S[0] == "z":
    print(-1)
elif len(S) == 26:
    for i in range(len(S)-1):
        if any([S[i] > S[i+j] for j in range(len(S)-i)]):
            ans = S[:i-1]+min(S[i:])
            break
    print(ans)
else:
    cn = Counter(S)
    po = []
    for char in string.ascii_lowercase:
        if char not in cn:
            po.append(char)
            break
        else:
            pass
    print(S+po[0])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python

ALPHA = sorted([a for a in "zyxwvutsrqponmlkjihgfedcba"])


def main():
    S = input()

    if 'zyxwvutsrqponmlkjihgfedcba' == S:
        return -1

    S = [s for s in S]
    ans = []
    for s in S:
        if s in ALPHA:
            ALPHA.remove(s)
            ans.append(s)
        else:
            ans.append(ALPHA.pop(0))
            break
    else:
        ans.append(ALPHA.pop(0))

    print("".join(ans))


if __name__ == '__main__':
    main()