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: Accepted

Solution code:
def inc(s):
	ans = s[:-1]
	# print(ans)
	# print(chr(ord(s[-1]) + 1))
	ans += chr(ord(s[-1]) + 1)
	return ans


s = input()

# if s[0] == 'z':
# 	print(-1)
# 	exit(0)

w = []
for i in range(26):
	w += chr(i + ord('a'))
# print(w)
for l in w:
	if l not in s:
		print(s + l)
		exit(0)


for i in range(1, 26):
	# incerc sa inlocuiesc ultimele i
	ans = s[:-i]
	# print("ans " + ans)
	# print(ans[:-1])
	for l in w:
		if l not in ans[:-1] and ord(l) > ord(ans[-1]):
			print(ans[:-1] + l)
			exit(0)
	# print(ans)
print(-1)

Submission status: Accepted

Solution code:
from sys import stdin
from string import ascii_lowercase
from bisect import bisect_left


def main():
    word = stdin.readline().rstrip()
    if word == ascii_lowercase[::-1]:
        print(-1)
        return

    spam = sorted(list(set(ascii_lowercase) - set(word)))
    if 1 <= len(spam):
        print(word + spam[0])
        return

    rword = word[::-1]
    tmp = []
    for x in rword:
        if len(tmp) != 0 and x < sorted(tmp)[-1]:
            idx = bisect_left(tmp, x)
            print(word[:-len(tmp) - 1] + sorted(tmp)[idx])
            return
        tmp.append(x)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

S = input()

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(S) == 26:
    for i in range(26):
        for j in range(ord(S[-i - 1]) + 1, 97 + 26):
            if chr(j) not in S[:-i - 1]:
                print(S[:-i - 1] + chr(j))
                sys.exit()
else:
    for i in range(97, 97 + 26):
        if chr(i) not in S:
            print(S + chr(i))
            break

Submission status: Accepted

Solution code:
from string import ascii_lowercase


def solve(s):
    if len(s) < 26:
        for c in ascii_lowercase:
            if c not in s:
                return s + c

    while s:
        pi = ascii_lowercase.index(s[-1])
        s = s[:-1]
        for c in ascii_lowercase[pi + 1:]:
            if c not in s:
                return s + c
    return -1


s = input()
print(solve(s))

Submission status: Accepted

Solution code:
S = input().rstrip()

alphabet = list('abcdefghijklmnopqrstuvwxyz')

# elongation
if len(S)<26:
    A = [a for a in alphabet if not a in S]
    print(S + A[0])
    exit()

#
for i in range(24, -1, -1):
    A = [a for a in alphabet if not a in S[:(i+1)]]
    for a in A:
        if S[i]<a:
            print(S[:i] + a)
            exit()

#
print(-1)

Submission status: Accepted

Solution code:
S = input()

alphabets = [chr(i) for i in range(97, 97+26)]
str_len = len(S)

if str_len < 26:
    for alphabet in alphabets:
        if alphabet not in S:
            print(S + alphabet)
            exit()
else:
    for i in range(-1, -str_len, -1):
        tmp_string = sorted(list(S[i:]))
        for tmp_str in tmp_string:
            if tmp_str > S[i - 1]:
                print(S[:i - 1] + tmp_str)
                exit()
print(-1)

Submission status: Accepted

Solution code:
S = input()
frag = False
for i in range(97, 123):
    if chr(i) not in S:
        print("".join([S, chr(i)]))
        frag = True
        break
if frag == False:
    for i in range(2, len(S) + 1):
        for j in range(97, 123):
            if chr(j) not in S[:1-i] and j > ord(S[1-i]):
                frag = True
                break
        if frag:
            break
    if frag:
        print("".join([S[:1-i], chr(j)]))
    else:
        if S[0] == "z":
            print(-1)
        else:
            print(chr(ord(S[0]) + 1))

Submission status: Accepted

Solution code:
# -- coding: utf-8 --
def find_word():
    word = input()
    alpha = [chr(i) for i in range(97, 97+26)]
    alpha_set = set(alpha)
    word_set = set(list(word))
    if alpha_set - word_set:
        for w in alpha:
            if not w in word:
                return (word + w)
    else:
        for i, w in enumerate(word[::-1]):
            new_word = word[:(-1 * (i + 1))]
            for j in range(1, 27):
                if 97 <= ord(w) + j <= 122 and not chr(ord(w) + j) in new_word:
                    return (new_word + chr(ord(w) + j))
    return -1

if __name__ == '__main__':
    print(find_word())

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import string
s = input()
rem = set(string.ascii_lowercase).difference(set(s))
answer = -1
for r in reversed(range(len(s) + 1)):
    if r < len(s):
        rem.add(s[r])
    for c in rem:
        t = s[: r] + c
        if s < t and (answer == -1 or t < answer):
            answer = t
print(answer)

Submission status: Accepted

Solution code:
S = input()
alp = "abcdefghijklmnopqrstuvwxyz"

if len(S) < 26:
  for i in range(len(alp)):
    c = alp[i]
    index = S.find(c)
    if index <0 :
      print(S,c,sep='')
      exit()

stack=[]
c_pre=""
for j in range(len(S)):
  c_now = S[25-j]
  #print(c_now)
  if c_now < c_pre :
    S = S[:-j-1]
    #print(S)
    c_min = "z"
    for cx in stack:
      if c_now < cx and cx <= c_min :
        c_min = cx
        #print(c_min)
    print(S,c_min,sep='')
    exit()
  else :
    stack.append(c_now)
    c_pre = c_now

print("-1")

Submission status: Accepted

Solution code:
def main():
    S = input()

    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
        return

    if len(S) < 26:
        for i in range(97, 123):
            if chr(i) not in S:
                print(S + chr(i))
                break
    else:
        for i in range(24, -1, -1):
            if S[i] != 'z':
                s = ord(S[i]) + 1
                for j in range(s, 123):
                    if chr(j) not in S[:i]:
                        print(S[:i] + chr(j))
                        return

main()

Submission status: Accepted

Solution code:
def ctoi(c):
    return ord(c) - 97


def itoc(i):
    return chr(i + 97)


def ans(S):
    l = len(S)
    if l != 26:
        alpha = [False] * 26
        for i in range(l):
            alpha[ctoi(S[i])] = True
        for j in range(26):
            if not alpha[j]:
                ret = S + itoc(j)
                return ret
    else:
        alpha = [True] * 26
        for i in range(l):
            if i == l-1:
                ret = str(itoc(ctoi(S[0])+1))
                # alpha[ctoi(S[0])+1] = True
                # alpha[ctoi(S[0])] = False
                # k = 0
                # while len(ret) < l:
                #     if not alpha[k]:
                #         ret += itoc(k)
                #     k += 1
                return ret
            else:
                for j in range(ctoi(S[l-i-1])+1, 26):
                    if not alpha[j]:
                        ret = S[:l-i-1] + itoc(j)
                        # alpha[j] = True
                        # alpha[ctoi(S[l-i-1])] = False
                        # k = 0
                        # while len(ret) < l:
                        #     if not alpha[k]:
                        #         ret += itoc(k)
                        #     k += 1
                        return ret
                alpha[ctoi(S[l-i-1])] = False


S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    print(ans(S))

Submission status: Accepted

Solution code:
from collections import defaultdict
from string import ascii_lowercase

s = input()
if s == 'zyxwvutsrqponmlkjihgfedcba' :
    print(-1)
elif len(s) < 26 :
    s = list(s)
    d = defaultdict(int)
    for c in s :
        d[c] += 1
    for c in ascii_lowercase :
        if d[c] == 0 :
            s.append(c)
            print(''.join(s))
            break
elif len(s) == 26 :
    s = list(s)
    d = defaultdict(int)
    for i in range(25, 0, -1) :
        if s[i-1] < s[i] :
            d[s[i]] = 1
            for c in ascii_lowercase :
                if d[c] == 1 and s[i-1] < c :
                    ans = s[0:i-1]
                    ans = ans + [c]
                    print(''.join(ans))
                    exit()
        elif s[i-1] > s[i] :
            d[s[i]] = 1

Submission status: Accepted

Solution code:
def Al(p):
   Alp = "abcdefghijklmnopqrstuvwxyz"
   for j in range(26):
      if p == Alp[j]:
         return j

def main():
   S = input()

   alpha = "abcdefghijklmnopqrstuvwxyz"

   if S == "zyxwvutsrqponmlkjihgfedcba":
      return -1

   elif len(S) != 26:
      for a in alpha:
         if (a in S)==False:
            return S + a
   
   else:
      A = ""
      for i in range(len(S)):
         s = S[-(i+1)]
         Snew = S[:-(i+1)]
         for a in A:
            if Al(s) < Al(a):
               return Snew + a
         A = A + s

if __name__ == "__main__":
   print(main())

Submission status: Accepted

Solution code:
def main():
    S = input()
    if len(S) >= 26:
        for i in range(24, -1, -1):
            if S[i] < S[i+1]:
                m = 'z'
                for j in S[i+1:]:
                    if j > S[i] and j < m:
                        m = j
                print(S[:i]+m)
                return
        print(-1)
    else:
        for i in range(ord('a'), ord('z')+1):
            flag = True
            for j in S:
                if j == chr(i):
                    flag = False
                    break
            if flag:
                print(S+chr(i))
                return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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']
word = list(input())

achars = alph[:]
for char in word:
    achars.remove(char)

if len(word) < 26:
    print(''.join(word)+achars[0])
else:
    ind = 26
    for i in range(1,26):
        if alph.index(word[i]) > alph.index(word[i-1]):
            ind = i
    if ind == 26:
        print(-1)
    else:
        word = word[:ind]
        for char in alph:
            if alph.index(char) > alph.index(word[ind-1]) and char not in word:
                word[ind-1] = char
                break
        print(''.join(word))

Submission status: Accepted

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

ABC = "abcdefghijklmnopqrstuvwxyz"

def add_next(s):    
    for c in ABC:
        if c not in s:
            return s + c
    raise Exception

def change_next(s):
    for i in reversed(range(n)):
        flag = False
        for j in range(i + 1, n):
            if ord(s[i]) < ord(s[j]):
                flag = True
                break
        if flag:
            for k in range(1, n):
                if chr(ord(s[i]) + k) not in s[:i]:
                    return s[:i] + chr(ord(s[i]) + k)
            raise Exception
    raise Exception

s = input()
n = len(s)

if n < 26:
    res = add_next(s)
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        res = "-1"
    else:
        res = change_next(s)

print(res)

Submission status: Accepted

Solution code:
import string

S = input()
N = len(S)

if S == string.ascii_lowercase[::-1]:
    print(-1)
elif N <= 25:
    for l in string.ascii_lowercase:
        if l not in S:
            print(S + l)
            break
else:
    ret = string.ascii_lowercase[::-1]
    for i in range(N):
        for c in string.ascii_lowercase:
            if c in S[:i]:
                continue
            s = S[:i] + c
            if S >= s:
                continue
            ret = min(ret, s)
    print(ret)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
S=input()
L=len(S)+1

ascii_lowercase=[chr(i) for i in range(97,97+26)]
#print(ascii_lowercase)

flag=0

S=S+'A'
for i in range(L):
    for letter in ascii_lowercase:
  #      print(S[:L-i-1],S[L-i-1],letter)
        if (letter not in S[:L-i-1]) and letter > S[L-i-1]:
            flag = 1
            answer = S[:L-i-1]+letter
            break
    if flag==1:
        break

if flag==1:
    print(answer)
else:
    print('-1')

Submission status: Accepted

Solution code:
import string

x = input()
if x == "zyxwvutsrqponmlkjihgfedcba":
	print(-1)
else:
	lis = list(x)
	letter = list(string.ascii_lowercase)
	
	if len(lis) != 26:
		for item in letter:
			if not item in lis:
				print(x+item)
				break
	else:
		n = 25
		box = []
		while n > 0:
			if lis[n] > lis[n-1]:
				box.append(lis.pop(n))
				set = "z"
				for st in box:
					if lis[n-1] < st and set >= st:
						set = st
				lis.pop(n-1)
				print("".join(lis)+set)
				break
			else:
				box.append(lis.pop(n))
			n -= 1

Submission status: Accepted

Solution code:
S = input()

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:

    code = list(map(lambda x : ord(x) - ord('a'), list(S)))

    done = False
    for i in range(26):
        if not i in code:
            code.append(i)
            done = True
            break


    while len(code) > 0 and not done:
        last = code[-1]
        code = code[:-1]
        if last == 25:
            continue
        else:
            for i in range(last+1, 26):
                if i not in code:
                    code.append(i)
                    done = True
                    break


    decode = list(map(lambda x : chr(x + ord('a')), code))
    print(''.join(decode))

Submission status: Accepted

Solution code:
s = input()
s2 = list(s)
ch = [chr(i) for i in range(97, 97+26)]
flg = 0
for k in ch:
	if not k in s2:
		s += k
		flg = 1
		break

if flg == 1:
	print(s)
	exit(0)
else:
	l = len(s)
	ans = ''
	for i in range(l-1,0,-1):
		for k in ch:
			if k <= s[i]:
				continue
			elif not k in s2[:i]:
				ans = s[:i] + k
				print(ans)
				exit(0)

	if s[0] == 'z':
		print(-1)
	else:
		print(chr(ord(s[0])+1))

Submission status: Accepted

Solution code:
S0 = input()
S = S0
C = set("abcdefghijklmnopqrstuvwxyz")
C1 = set(S)
candidate = sorted(C - C1)
if candidate != []:
    if S0 < S + candidate[0]:
        print(S + candidate[0])
        exit()
while S != "":
    C1 = set(S)
    candidate = sorted([ c for c in C - C1 if c > S[-1]])
    if candidate != []:
        if S0 < S[0:-1] + candidate[0]:
            print(S[0:-1] + candidate[0])
            exit()
    S = S[0:- 1]
if S0[0] == "z":
    print(-1)
else:
    print(chr(ord(S0[0]) + 1))

Submission status: Accepted

Solution code:
def _agc022_a():
    S = input()
    al = 'abcdefghijklmnopqrstuvwxyz'
    if len(S) < 26:
        for c in al:
            if not c in S:
                print(S+c)
                return
    elif S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        T = S
        A = [T[-1]]
        T = T[:-2]

        while True:
            for c in A:
                if S < T+c:
                    print(T+c)
                    return

            A += T[-1]
            T = T[:-1]

_agc022_a()

Submission status: Accepted

Solution code:
# encoding:utf-8


def main(s):
	alphabet = "abcdefghijklmnopqrstuvwxyz"
	d = {alphabet[i]:i for i in range(26)}
	if s == "zyxwvutsrqponmlkjihgfedcba":
		print("-1")
	elif len(s) < 26:
		for i in range(26):
			if alphabet[i] not in s:
				print(s + alphabet[i])
				break
	else:
		tail = [d[s[25]]]
		for i in range(0, 25):
			if d[s[24 - i]] >= max(tail):
				tail.append(d[s[24 - i]])
				continue
			else:
				tmp = min([x for x in tail if x >= d[s[24-i]]])
				print(s[0:(24-i)] + alphabet[tmp])
				break


if __name__ == '__main__':
	s = input()
	main(s)

Submission status: Accepted

Solution code:
S = input()
alphabet = ['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']
for i in alphabet:
    if i not in S:
        print(S + i)
        break
else:
    output = False
    while len(S) > 0 and not output:
        last = alphabet.index(S[-1])
        for i in alphabet[last + 1:]:
            if i not in S[:-1]:
                print(S[:-1] + i)
                output = not(output)
                break
        else:
            S = S[:-1]
    if not output:
        print(-1)

Submission status: Accepted

Solution code:
from string import lowercase
s = raw_input().strip()
if len(s) < 26:
    for c in lowercase:
        if c not in s:
            print s + c
            break
else:
    c = ' '
    for i in xrange(25, -1, -1):
        if s[i] < c:
            s = s[:i+1]
            for c in lowercase:
                if c > s[-1] and c not in s:
                    print s[:-1] + c
                    quit()
        c = s[i]
    print -1

Submission status: Accepted

Solution code:
S = input()

abc = [chr(i) for i in range(97,97+26)]
abc2 = abc
end = 0

if len(S) == 26:
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)#
    else:
        for i in range(1, 26):
            for j in abc[abc.index(S[-(i+1)])+1:]:
                if j in S[-i:]:
                    print(S[:-(i+1)]+j)
                    end = 1
                    break
            if end == 1:
                break
        else:
            print(-1)#
                
    
else:
    for i in S:
        abc.remove(i)
    print(S+abc[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))
from collections import Counter
from string import ascii_lowercase

S = input()
C = Counter(list(S))
D = {k:i for i, k in enumerate(ascii_lowercase)}

for a in list(ascii_lowercase):
    if C[a] == 0:
        S += a
        print(S)
        break
else:
    nokori = [D[S[-1]]]    
    for i in range(24, -1, -1):
        res = 30
        for n in nokori:
            if D[S[i]] < n:
                res = min(res, n)

        if res == 30:
            nokori.append(D[S[i]])            
        else:
            S = S[:i] + ascii_lowercase[res]
            print(S)
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
j = input()
lis = sorted(list("qwertyuiopasdfghjklzxcvbnm"))
abs = ""
for h in j:
    lis.remove(h)
if not lis:
    ans = "-1"
else:
    ans = "{}{}".format(j, lis[0])
tmp = "zz"
for i in range(len(j)):
    try:
        if tmp > j[:i] + sorted(filter(lambda x: x > j[i], j[i+1:]))[0]:
            tmp = j[:i] + sorted(filter(lambda x: x > j[i], j[i+1:]))[0]
    except:
        pass
if tmp == "zz" and ans == "-1":
    print("-1")
elif tmp == "zz":
    print(ans)
elif ans == "-1":
    print(tmp)
else:
    print(min(ans, tmp))

Submission status: Accepted

Solution code:
from collections import Counter
S = input()
ct = Counter(S)
if len(ct) != 26:
  for i in range(26):
    c = chr(ord('a')+i)
    if c not in ct:
      print(S+c)
      exit()

prev = chr(ord('a')-1)
arr = []
for i in range(26)[::-1]:
  if prev > S[i]:
    print(S[:i] + min(filter(lambda x: x > S[i],arr)))
    break
  arr.append(S[i])
  prev = S[i]
else:
  print(-1)

Submission status: Accepted

Solution code:
def main():
    s = input().strip()
    if len(s) < 26:
        for i in range(ord('a'), ord('z') + 1):
            c = chr(i)
            if c not in s:
                print(s + c)
                return
    for i in range(24, -1, -1):
        c = s[i]
        ss = s[:i]
        for j in range(ord(c) + 1, ord('z') + 1):
            cc = chr(j)
            if cc not in ss:
                print(ss + cc)
                return
    print(-1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
if s == 'zyxwvutsrqponmlkjihgfedcba':
  print(-1)
  exit()
L = 26
s = [ord(c) - ord('a') for c in s]


memo = [False]*L
for c in s:
  memo[c] = True

lowerbound = 0
while all(memo[lowerbound:]):
  c = s.pop()
  lowerbound = c + 1
  memo[c] = False

for i in range(lowerbound,L):
  if not memo[i]:
    s.append(i)
    break

print(''.join(map(chr, [c + ord('a') for c in s])))

Submission status: Accepted

Solution code:
INF = 999999999999999999L
EPS = 1e-12

def read():
    return raw_input().strip()

def read_ints():
    return map(int,read().split())

s = read()
if len(s) < 26:
    for i in range(26):
        c = chr(ord("a")+i)
        if c not in s:
            print s+c
            break 
            
else:
    for i in reversed(range(len(s))):
        c = ord(s[i]) - ord("a")
        found = False
        for j in range(c+1,26):
            cc = chr(ord("a")+j)
            if cc not in s[:i]:
                print s[:i]+cc 
                found = True 
                break 
        if found:
            break
    if not found:
        print -1

Submission status: Accepted

Solution code:
S = raw_input()
A = [chr(ord("a")+i) for i in range(26)]
if len(S)<26:
  for a in A:
    if a not in S:
      print S+a
      exit(0)
else:
  for p in range(26)[::-1]:
    for a in A[A.index(S[p])+1:]:
      if a not in S[:p]:
        print S[:p]+a
        exit(0)
  print -1

Submission status: Accepted

Solution code:
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
def hoge(string, least = 'Z'):
    for c in ALPHABET:
        if c not in string:
            if c <= least:continue
            return string + c
    if string == "":
        return "-1"
    return hoge(string[:-1], string[-1])

print hoge(raw_input())

Submission status: Runtime Error

Solution code:
S=list(input())
m=list('abcdefghijklmnopqrstuvwxyz')[::-1]

if len(S)<26:
  for i in m[::-1]
        if i not in S:
            S.append(i)
            break
    print(''.join(S))
else:
    x=S[-1]
    for i in range(24,-1,-1):
        if x>S[i]:
            y=m.index(S[i])
            S=S[:i]
            for j in range(y-1,-1,-1):
                if m[j] not in S:
                    S.append(m[j])
                    break
            break
        else:
            x=S[i]
        
        if i==0:
            print(-1)
            exit()
    print(''.join(S))

Submission status: Runtime Error

Solution code:
S=input()
alpha='abcdefghijklmnopqrstuvwxyz'
ans=''
P=[]
for i in range(26):
    P.append([alpha[i],1])


cnt=0
if len(S)==26:
    l=[]
    poe=S[::-1]
    for i in range(25):
        if poe[i]<poe[i+1]:
            cnt+=1
            l.append(poe[i])
        else:
            ans=poe[:i+1:-1]+min(l)
            break
    if cnt==25:
        ans='-1'
else:
    for i in range(len(S)):
        for j in range(26):
            if S[i]==P[j][0]:
                P[j][1]=0
        for i in range(26):
            if P[i][1]==1:
                ans=S+P[i][0]
                break

print(ans)

Submission status: Runtime Error

Solution code:
s=input()
if s=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
  exit()
data='abcdefghijklmnopqrstuvwxyz'
if len(s)!=26:
  for i in range(26):
    if data[i] not in s:
      print(s+data[i])
      exit()
else:
  res=[]
  i=0
  while s[25-i]<s[24-i]:
    res.append(s[25-i])
    i+=1
  print(s[:24-i]+res[0])

Submission status: Runtime Error

Solution code:
import sys, math
from functools import lru_cache
from collections import defaultdict
from decimal import Decimal
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    S = input()
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
        return

    l = [0]*26
    for c in S:
        l[ord(c)-ord('a')] += 1

    if len(S) < 26:
        i = l.index(0)
        print(S+chr(i+ord('a')))
        return

    for i in range(len(S)):
        if S[i] > S[i+1]:
            break

    c = min(S[i+1:])
    print(S[:i-1]+c)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s = list(input())
m = list('zyxwvutsrqponmlkjihgfedcba')
if len(s) < 26:
    for i in m[::-1]:
        if not j in s:
            s.append(j)
            break
    print(''.join(s))
else:
    if s == m:
        print(-1)
    else:
        x = s[-1]
        for i in range(25)[::-1]:
            if x > s[i]:
                s = s[:i]
                s.append(x)
                break
            if i == 0:
                z = m.index(s[0])
                s = m[z-1]
                break
        print(''.join(s))

Submission status: Runtime Error

Solution code:
s = list(input())
m = list('zyxwvutsrqponmlkjihgfedcba')
if len(s) < 26:
    for i in m:
        if not i in s:
            s.append(_)
            break
    print(''.join(s))
else:
    if s == m:
        print(-1)
    else:
        x = s[-1]
        for i in range(25)[::-1]:
            if x > s[i]:
                s = s[:i]
                s.append(i)
                break
            if i == 0:
                z = m.index(s[0])
                s = m[z-1]
        print(''.join(s))

Submission status: Runtime Error

Solution code:
s = input(); a = []; b = [0]*27; t = ""
for i in range(len(s)): a.append(ord(s[i])); b[a[i]-97] = 1
if s == "zyxwvutsrqponmlkjihgfedcba": print(-1)
else:
    for i in range(27):
        if b[i] == 0: c = i; break
    if c != 26:
        a.append(c+97)
        for i in range(len(a)): t += chr(a[i])
        print(t)
    else:
        d = []
        for _ in range(24):
            if a[-2] > a[-1]: d.append(a.pop())
            else:
                d.append(a.pop()); a.pop(); d.sort()
                break
        else: d.append(a.pop()); a.pop(); d.sort()
        for i in range(len(d)):
            if a[-1] < d[i]: a.append(d[i]); break
        for i in range(len(a)): t += chr(a[i])
        print(t)

Submission status: Runtime Error

Solution code:
s = input(); a = []; b = [0]*27; t = ""
for i in range(len(s)): a.append(ord(s[i])); b[a[i]-97] = 1
if s == "zyxwvutsrqponmlkjihgfedcba": print(-1)
else:
    for i in range(27):
        if b[i] == 0: c = i; break
    if c != 26:
        a.append(c+97)
        for i in range(len(a)): t += chr(a[i])
        print(t)
    else:
        d = []
        for _ in range(25):
            if a[-2] > a[-1]: d.append(a.pop())
            else:
                d.append(a.pop()); a.pop(); d.sort()
                for i in range(len(d)):
                    if a[-1] < d[i]: a.append(d[i]); break
                break
        for i in range(len(a)): t += chr(a[i])
        print(t)

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline
S =  input()
S = S.replace("\n","")
s_list = list(S)
alpha_list = [chr(i) for i in range(97, 97+26)]
flag_list = [False for _ in range(23)]


if len(s_list) == 26:
    for i in range(25, 0, -1):
        if s_list[i - 1] < s_list[i]:
            c = 'ω'
            for j in range(i, 26):
                if s_list[i - 1] < s_list[j]:
                    c = min(c, s_list[j])
            print(S[:i-1] + c)
            exit(0)
    print(-1)

else:
    for i,alpha in enumerate(alpha_list):
        if alpha in s_list:
            flag_list[i] = True
    for i,flag in enumerate(flag_list):
        if not flag:
            print(S + alpha_list[i])
            break

Submission status: Runtime Error

Solution code:
import sys
import heapq
from decimal import Decimal

input = sys.stdin.readline
S =  input()
S = S.replace("\n","")
s_list = list(S)
alpha_list = [chr(i) for i in range(97, 97+26)]
flag_list = [False for _ in range(23)]


if len(s_list) == 26:
    for i in range(25, 0, -1):
        if s_list[i - 1] < s_list[i]:
            c = 'ω'
            for j in range(i, 26):
                if s_list[i - 1] < s_list[j]:
                    c = min(c, s_list[j])
            print(S[:i-1] + c)
            exit()
    print(-1)

else:
    for i,alpha in enumerate(alpha_list):
        if alpha in s_list:
            flag_list[i] = True
    for i,flag in enumerate(flag_list):
        if not flag:
            print(S + alpha_list[i])
            break

Submission status: Runtime Error

Solution code:
import sys
import heapq
from decimal import Decimal

input = sys.stdin.readline
S =  input()
S = S.replace("\n","")
s_list = list(S)
alpha_list = [chr(i) for i in range(97, 97+26)]
flag_list = [False for _ in range(23)]


if len(s_list) == 26:
    for i in range(25, 0, -1):
        if s_list[i - 1] < s_list[i]:
            c = 'ω'
            for j in range(i, 26):
                if s_list[i - 1] < s_list[j]:
                    c = min(c, s_list[j])
            print(s_list[:i-1] + c)
            exit()
    print(-1)

else:
    for i,alpha in enumerate(alpha_list):
        if alpha in s_list:
            flag_list[i] = True
    for i,flag in enumerate(flag_list):
        if not flag:
            print(S + alpha_list[i])
            break

Submission status: Runtime Error

Solution code:
S = input()
if len(S) == 26:
    done = set()
    for i, s in enumerate(S[::-1]):
        s = ord(s)
        if done:
            if max(done) < s:
                done.add(s)
                if i == 25:
                    print(-1)
            elif i != 25:
                print(S[: -(i + 1)] + chr(min(done)))
                break
            else:
                print(chr(ord(S[0] + 1)))
        else:
            done.add(s)
else:
    for i in range(ord("a"), ord("z") + 1):
        if not (chr(i) in S):
            print(S + chr(i))
            break

Submission status: Runtime Error

Solution code:

S = input()
L = list(map(ord, S))
if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        pre = set()
        for i, l in enumerate(L[::-1]):
            if pre:
                if min(pre) > l:
                    print(reversed(reversed(S)[i + 1 :]) + chr(min(pre)))
                    break
            pre.add(l)
else:
    for _ in range(ord("a"), ord("z") + 1):
        if not (i in L):
            break
    print(S + chr(i))

Submission status: Runtime Error

Solution code:
s = list(input())
n = list('abcdefghijklmnopqrstuvwxyz')
if len(s) < 26:
    for _ in m:
        if not _ in s:
            s.append(_)
            break
    print(''.join(s))
else:
    if s == n[::-1]:
        print(-1)
    else:
        x = s[-1]
        for _ in range(25)[::-1]:
            if x > s[_]:
                s = s[:_]
                s.append(x)
                break
        print(''.join(s))