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:
    # coding: utf-8
# Your code here!
eng_li = []
removed_list = []
all_letter = 26
for i in range (97,97+all_letter):
    eng_li.append(chr(i))

string = list(raw_input().strip())
length = len(string)
if length < all_letter:
    for i in string:
        eng_li.remove(i)
    string.append(eng_li[0])
    answer = "".join(string)
else:
    for i in range(all_letter-1):
        if ord(string[-1]) < ord(string[-2]) :
            removed_list.append(string[-1])
            string.pop()
        else:
            removed_list.append(string[-1])
            string.pop()
            removed_list=filter(lambda x:x>string[-1],sorted(removed_list))
            string.pop()
            string.append(removed_list[0])
            answer ="".join(string)
            break
    else:
        answer = -1
print answer

Submission status: Runtime Error

Solution code:
   # coding: utf-8
# Your code here!
eng_li = []
removed_list = []
all_letter = 26
for i in range (97,97+all_letter):
    eng_li.append(chr(i))

string = list(raw_input().strip())
length = len(string)
if length < all_letter:
    for i in string:
        eng_li.remove(i)
    string.append(eng_li[0])
    answer = "".join(string)
else:
    for i in range(all_letter-1):
        if ord(string[-1]) < ord(string[-2]) :
            removed_list.append(string[-1])
            string.pop()
        else:
            removed_list.append(string[-1])
            string.pop()
            removed_list=filter(lambda x:x>string[-1],sorted(removed_list))
            string.pop()
            string.append(removed_list[0])
            answer ="".join(string)
            break
    else:
        answer = -1
print answer

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
eng_li = []
removed_list = []
all_letter = 5
for i in range (97,97+all_letter):
    eng_li.append(chr(i))

string = list(raw_input().strip())
length = len(string)
if length < all_letter:
    for i in string:
        eng_li.remove(i)
    string.append(eng_li[0])
    answer = "".join(string)
else:
    for i in range(all_letter-1):
        if ord(string[-1]) < ord(string[-2]) :
            removed_list.append(string[-1])
            string.pop()
        else:
            removed_list.append(string[-1])
            string.pop()
            print string[-1]
            removed_list=filter(lambda x:x>string[-1],sorted(removed_list))
            print removed_list
            string.pop()
            string.append(removed_list[0])
            answer ="".join(string)
            break
    else:
        answer = -1
print answer

Submission status: Runtime Error

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

if len(S) != 26:
    for i in range(len(S)):
        count[S[i]] += 1
    
    print(S, end='')

    for key in count.keys():
       if count[key] == 0:
           print(key)
           break
        
else:
    i = 0
    for key in count.key():
        if count[key] == S[i]:
            print(S[i], end='')
        else:
            print(count[key])
            break
        i += 1

Submission status: Runtime Error

Solution code:
import string

S = input()
if len(S) < 26:
    for c in string.ascii_lowercase:
        if c not in S:
            break
    print(S + c)
else:
    ret = None
    for i in range(26):
        if max(S[25-i:]) != S[25-i]:
            mc = 'z'
            for c in S[25-i:]:
                if c > S[25-i]:
                    mc = min(c, mc)
            ret = S[:25-i] + mc
            break
            
    if ret is None:
        print(-1)
    else:
        print(ret)
S = input()
if len(S) < 26:
    for c in string.ascii_lowercase:
        if c not in S:
            break
    print(S + c)
else:
    ret = None
    for i in range(26):
        if max(S[25-i:]) != S[25-i]:
            mc = 'z'
            for c in S[25-i:]:
                if c > S[25-i]:
                    mc = min(c, mc)
            ret = S[:25-i] + mc
            break
            
    if ret is None:
        print(-1)
    else:
        print(ret)

Submission status: Runtime Error

Solution code:
S = input()
if len(S) < 26:
    for c in string.ascii_lowercase:
        if c not in S:
            break
    print(S + c)
else:
    ret = None
    for i in range(26):
        if max(S[25-i:]) != S[25-i]:
            mc = 'z'
            for c in S[25-i:]:
                if c > S[25-i]:
                    mc = min(c, mc)
            ret = S[:25-i] + mc
            break
            
    if ret is None:
        print(-1)
    else:
        print(ret)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

int main(){

	std::string str;

	std::cin >> str;

	bool used_c[26] = {};

	if(str == "zyxwvutsrqponmlkjihgfedcba"){
		std::cout << -1 << std::endl;
		return 0;
	}

	std::vector<std::string> strs;
	if(str.length() == 26){
		for(int i = 0; i < 26; ++i){
			for(int j = 0; j < 26; ++j){
				if(!used_c[j] && str[i] - 'a' < j){
					strs.push_back(str.substr(0, i) + char('a' + j));
				}
			}
			used_c[str[i] - 'a'] = true;
		}
	}
	else{
		for(int i = 0; i < str.length(); ++i){
			used_c[str[i] - 'a'] = true;
		}
		for(int i = 0; i < 26; ++i){
			if(!used_c[i]){
				std::cout << str << char('a' + i) << std::endl;
				return 0;
			}
		}
	}

	std::sort(strs.begin(), strs.end());
	std::cout << strs[0] << std::endl;

	return 0;
}

Submission status: Runtime Error

Solution code:
n=input()
ans=[3,5]
for i in xrange(n - 2):
 ans.append(i * 2 + 2)
s = sum(ans)
for i in xrange(30000, -2, 0):
 if (s + i) % 15 == 0:
  ans.sort()
  print ' '.join(ans)
  exit()

Submission status: Runtime Error

Solution code:
S = input()
C = [chr(97+i) for i in range(26)]


if S == "".join(C.sort(reverse=True)):
    print(-1)

elif len(S) != 26:
    for c in C:
        if c not in S:
            print(S + c)
            break
else:
    K = []
    for i in range(25, -1, -1):
        K.append(S[i])
        K.sort()
        for j in range(25, -1, -1):
            if K[0] > S[j]:
                print(S[:j])
                exit()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

alp = 'abcdefghijklmnopqrstuvwxyz'
S = input()
if S == alp[::-1]:
    print(-1)
else:
    if len(S) == len(alp):
        p = ''
        for i, j in zip(alp, S):
            if i != j : break
            p += i
        S = p

    for i in set(S) : alp = alp.replace(i, '')
    print(S+alp[0])

Submission status: Runtime Error

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

# input
S = input()
# S = "abcdefghijklmnopqrstuvwzyx"
# S = "atcoder"

s = list(S)
a = []
for i in range(len(s)):
    a.append(ord(s[i]))

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


# function
def func(s_):
    ss_ = []
    for i in range(len(s_)):
        ss_.append(chr(s_[i]))
    ss_ = ''.join(ss_)
    return ss_


# solve
if (len(s) < 26):
    for i in range(len(s)):
        b.remove(a[i])
    a.append(min(b))
    print(func(a))

if (len(s) == 26):
    for i in reversed(range(len(s))):
        if (a[i] < a[i-1]):
            if (i == 1):
                print(-1)
                break
            else:
                continue
        else:
            c = a[0:i]
            d = a[i:len(a)]
            # a[i-1]よりも大きくて最も小さいものを探す
            d.pop()
            d.append(min(d))
            print(func(d))

Submission status: Runtime Error

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

# input
S = input()
# S = "abcdefghijklmnopqrstuvwzyx"
# S = "atcoder"

s = list(S)
a = []
for i in range(len(s)):
    a.append(ord(s[i]))

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


# function
def func(s_):
    ss_ = []
    for i in range(len(s_)):
        ss_.append(chr(s_[i]))
    ss_ = ''.join(ss_)
    return ss_


# solve
if (len(s) < 26):
    for i in range(len(s)):
        b.remove(a[i])
    a.append(min(b))
    print(func(a))

if (len(s) == 26):
    for i in reversed(range(len(s))):
        if (a[i] < a[i-1]):
            if (i == 1):
                print(-1)
            else:
                continue
        else:
            a = a[0:i]
            # ここから26文字未満の場合とほとんど同じ
            for i in range(len(a)):
                b.remove(a[i])
            a.pop()
            a.append(min(b))
            print(func(a))
            break

Submission status: Runtime Error

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

# input
S = input()
# S = "abcdefghijklmnopqrstuvwzyx"
# S = "atcoder"

s = list(S)
a = []
for i in range(len(s)):
    a.append(ord(s[i]))

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


def func(s_):
    ss_ = []
    for i in range(len(s_)):
        ss_.append(chr(s_[i]))
    ss_ = ''.join(ss_)
    return ss_


# solve
if (len(s) < 26):
    for i in range(len(s)):
        b.remove(a[i])
    a.append(min(b))
    print(func(a))

if (len(s) == 26):
    print(a)
    for i in reversed(range(len(s))):
        if (a[i] < a[i-1]):
            if (i == 1):
                print(-1)
            else:
                continue
        else:
            a = a[0:i]
            # ここから26文字未満の場合とほとんど同じ
            for i in range(len(a)):
                b.remove(a[i])
            a.pop()
            a.append(min(b))
            print(func(a))
            break

Submission status: Runtime Error

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

# input
S = input()
# S = "abcdefghijklmnopqrstuvwzyx"
# S = "atcoder"

s = list(S)
a = []
for i in range(len(s)):
    a.append(ord(s[i]))

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


def func(s_):
    ss_ = []
    for i in range(len(s_)):
        ss_.append(chr(s_[i]))
    ss_ = ''.join(ss_)
    return ss_


# solve
if (len(s) < 26):
    for i in range(len(s)):
        b.remove(a[i])
    a.append(min(b))
    print(func(a))

if (len(s) == 26):
    print(a)
    for i in reversed(range(len(s))):
        print(i)
        if (a[i] < a[i-1]):
            if (i == 1):
                print(-1)
            else:
                continue
        else:
            a = a[0:i]
            print(a)
            # ここから26文字未満の場合とほとんど同じ
            for i in range(len(a)):
                b.remove(a[i])
            a.pop()
            a.append(min(b))
            print(func(a))
            break

Submission status: Runtime Error

Solution code:
#problem A

S = input()
letters = "abcdefghijklmnopqrstuvwxyz"


def find_next_letter(S):
    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 == letters[::-1]:
        print(-1)
    elif len(S) == 26:
        print(new_lex_string(S))
    else:
        for i in range(len(S) - 1, 0, -1):
            if S[i - 1] > S[i]:
                pass
            else:
                break

        for a in sorted(set(letters) - set(S[:i])):
            if a > S[i-1]:
                print(S[:i-1] + a)

        
main()

Submission status: Runtime Error

Solution code:
s = list(str(input()))


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

nosame = 26
lastal = 0
nexts = ''
changeal = 'z'

if len(s) is 26:
    for k,i in enumerate(reversed(s)):
        if k is 25:
#            print(-1)
        elif i is alpha[k]:
            nosame = 25-k
            continue
        else:
            break

    for k,i in enumerate(reversed(s)):
        if k < 26-nosame:
            continue
        if i < s[nosame-1]:
            lastal = 25-k
            changeal = i
            break
    for i in range(lastal):
        nexts += s[i]
    nexts += s[nosame-1]
    print(nexts)

else:
    for i in s:
        alpha[alpha.index(i)] = 'z'

    for i in alpha:
        changeal = min(changeal,i)

    for i in s:
        nexts += i
    nexts += changeal
    print(nexts)

Submission status: Runtime Error

Solution code:
import string
alphabets = string.ascii_lowercase

S = input()

if len(S) < 26:
    for i in range(26):
        if alphabets[i] not in S:
            char_idx = i
            break
    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
            
    if idx == 0:
        for i in range(26):
            if alphabets[i] > S[idx]:
                char_idx = i
                break
        print(alphabets[char_idx])
    else:
        st = S[:idx]
        char = S[idx-1]
        len_st = len(st)
        for i in range(26):
            if alphabets[i] not in st and alphabets[i] > char:
                char_idx = i
                break
        print(st[:len_st-1] + alphabets[char_idx])

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:
# -*- coding: utf-8 -*-

# input
S = str(input())
# S = "abcdefghijklmnopqrstuvwzyx"

s = list(S)
ss = []
for i in range(len(s)):
    ss.append(ord(s[i]))
a = list(ss)

t = [chr(i) for i in range(97, 97+26)]
tt = [i for i in range(97, 97+26)]
b = list(tt)


def func(s_):
    ss_ = []
    for i in range(len(s_)):
        ss_.append(chr(s_[i]))
    ss_ = ''.join(ss_)
    return ss_


# solve
if (len(ss) < 26):
    for i in range(len(s)):
        b.remove(ss[i])
    a.append(min(b))
    print(func(a))

if (len(ss) == 26):
    if (S == "zyxwvutsrqponmlkjihgfedcba"):
        print(-1)
    else:
        for i in range(len(s)):
            if (a[i] == b[i]):
                continue
            else:
                c = a[0:i] + [b[i]]
                break
        print(func(c))

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)

Submission status: Runtime Error

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("abcdefghijklmnopqrstuvwxz")


agc022A()

Submission status: Runtime Error

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

# input
S = input()
# S = "abcdefghijklmnopqrstuvwzyx"

s = list(S)
ss = []
for i in range(len(s)):
    ss.append(ord(s[i]))
a = list(ss)

t = [chr(i) for i in range(97, 97+26)]
tt = [i for i in range(97, 97+26)]
b = list(tt)


def func(s_):
    ss_ = []
    for i in range(len(s_)):
        ss_.append(chr(s_[i]))
    ss_ = ''.join(ss_)
    return ss_


# solve
if (len(ss) < 26):
    for i in range(len(s)):
        b.remove(ss[i])
    a.append(min(b))
    print(func(a))

if (len(ss) == 26):
    if (S == "zyxwvutsrqponmlkjihgfedcba"):
        print(-1)
    else:
        for i in range(len(s)):
            if (a[i] == b[i]):
                continue
            else:
                c = a[0:i] + [b[i]]
                break
        print(func(c))

Submission status: Runtime Error

Solution code:
# encoding:utf-8

import fractions

def check(l):
	l = list(map(int, l.split()))
	print(len(l), len([x for x in l if x % 2 == 0]), len([x for x in l if x % 2 == 1]))
	if max(l) >= 30001:
		print("max >= 30001")
		return False

	if len(set(l)) != len(l):
		print("not unique")
		return False

	tmp = l[0]


	for i in range(len(l) - 1):
		tmp = fractions.gcd(tmp, l[i+1])
	if tmp != 1:
		print("gcd is 1")
		return False 

	for i in range(len(l)):
		if fractions.gcd(l[i], sum(l) - l[i]) == 1:
			print("not special")
			return False

	return True


def main(n):
	if n == 3:
		return "2 5 63"

	sub1 = [str(i * 3) for i in range(1, 10001) if i % 2 == 1]
	sub2 = [str(i) for i in range(1, 30000) if i % 2 == 0 and i % 3 != 0]
	sub3 = [str(i) for i in range(1, 30001) if i % 6 == 0]
	if n <= 5003:
		if n % 2 == 0:
			s = "2 4 " + " ".join([str(x) for x in sub1[0:(n-2)]])
			return(s)
		else:
			s = "2 4 6 " + " ".join([str(x) for x in sub1[0:(n-3)]])
			return(s)
	elif n <= 15000:
		if n % 2 == 0:
			s = " ".join(sub2[0:(n-5000)]) + " " +  " ".join(sub1)
			return(s)
		else:
			s = " ".join(sub2[0:(n-5001)]) + " 6 " +  " ".join(sub1)
			return(s)
	else:
		s = " ".join(sub2) + " " +  " ".join(sub1) + " " + " ".join(sub3[0:(n-15000)]) 
		return(s)

if __name__ == '__main__':
	n = int(input())
	#print(check(main(n)))
	#print(main(20000))
	print(main(n))
	#print(check("2 4 3 20001"))
	#for i in range(5001, 5006):
	#	ans = main(i)
	#	print(i, check(ans))

Submission status: Runtime Error

Solution code:
import string
alphabets = string.ascii_lowercase

S = input()

if len(S) < 26:
    for i in range(26):
        if alphabets[i] not in S:
            char_idx = i
            break
    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
            
    if idx == 0:
        for i in range(26):
            if alphabets[i] > S[idx]:
                char_idx = i
                break
        print(alphabets[char_idx])
    else:
        st = S[:idx]
        if S[idx] == 'z':
            char = S[idx-1]
            len_st = len(st)
            for i in range(26):
                if alphabets[i] not in st and alphabets[i] > char:
                    char_idx = i
                    break
            print(st[:len_st-1] + alphabets[char_idx])
        else:
            char = S[idx]
            for i in range(26):
                if alphabets[i] not in st and alphabets[i] > char:
                    char_idx = i
                    break
            print(st + alphabets[char_idx])

Submission status: Runtime Error

Solution code:
alphabet='abcdefghijklmnopqrstuvwxyz'

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

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

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

S=input()

print(next_word(S,alphabet))

Submission status: Runtime Error

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()]
        alfa = remainings[0]

        out = S + alfa
        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


agc022A()

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:
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]:
                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)