Problem:


Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx



Sample input:
atcoder


Sample output:
atcoderb

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    dic = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
           'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    for d in dic:
        if d in S:
            continue
        else:
            return print(S + d)
    if S == "zyxwvutsrqponmlkjihgfedcba":
        return print(-1)
    pre = ord(S[0])
    for i, s in enumerate(S[1:]):
        if pre + 1 != ord(s):
            return print(S[:i] + chr(pre + 1))
        pre = ord(s)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=input()

if len(set(s))!=len(s):
    print(-1)
    exit()
dic_w=''
for i in 'abcdefghijklmnopqrstuvwxyz':
    if i not in s:
        dic_w=i
        print(s+dic_w)
        exit()

print(-1)

Submission status: Wrong Answer

Solution code:
s = input()

if len(s) == 26:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        exit()
    else:
        u = ord(s[25]) - ord("a")
        p = ord(s[25]) - ord("a")
        for i in range(24, 0, -1):
            v = ord(s[i]) - ord("a")
            if u < v:
               p = min(p, ord(s[i]) - ord("a"))
               u = v
            else:
                print(s[:i] + chr(p+ord("a")))
                exit()
                break
        
        

a = [0 for _ in range(26)]

for i in range(len(s)):
    a[ord(s[i]) - ord("a")] += 1

ans = s
for i in range(26):
    if a[i] == 0:
        ans += chr(i+ord("a"))
        break

print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
alpha = [chr(i) for i in range(97, 97+26)]
if len(S) == 26:
    ans = list(S)
    x = S[-1]
    y = []
    y.append(x)
    check = 0
    for i in range(2,26):
        if alpha.index(x) > alpha.index(S[-1*i]):
            ans = S[:-1*i]
            for j in alpha:
                if j not in ans and alpha.index(j) > alpha.index(S[-1*i]):
                    ans.append(j)
                    check = 1
                    break
        if check == 1:
            break
        x = S[-1*i]
        y.append(x)
        y.sort()
else:
    ans = list(S)
    for i in alpha:
        if i not in S:
            ans.append(i)
            break
    #print(ans,S)
if ans == S:
    print(-1)
else:   
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = list(input())
alpha = [chr(i) for i in range(97, 97+26)]
if len(S) == 26:
    ans = list(S)
    x = S[-1]
    y = []
    y.append(x)
    check = 0
    for i in range(2,26):
        if alpha.index(x) > alpha.index(S[-1*i]):
            ans = S[:-1*i]
            for j in alpha:
                if j not in ans and j != S[-1*i]:
                    ans.append(j)
                    check = 1
                    break
        if check == 1:
            break
        x = S[-1*i]
        y.append(x)
        y.sort()
else:
    ans = list(S)
    for i in alpha:
        if i not in S:
            ans.append(i)
            break
    #print(ans,S)
if ans == S:
    print(-1)
else:   
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = list(input())
alpha = [chr(i) for i in range(97, 97+26)]
if len(S) == 26:
    ans = list(S)
    x = S[-1]
    y = []
    y.append(x)
    for i in range(2,26):
        if alpha.index(x) < alpha.index(S[-1*i]):
            ans = S[:-1*i]
            ans.append(y[0])
        x = S[-1*i]
        y.append(x)
        y.sort()
else:
    ans = list(S)
    for i in alpha:
        if i not in S:
            ans.append(i)
            break
    #print(ans,S)
if ans == S:
    print(-1)
else:   
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = list(input())
alpha = [chr(i) for i in range(97, 97+26)]
if len(S) == 26:
    ans = list(S)
    x = S[-1]
    y = []
    y.append(x)
    for i in range(2,26):
        if alpha.index(x) > alpha.index(S[-1*i]):
            ans = S[:-1*i]
            ans.append(y[0])
            break
        x = S[-1*i]
        y.append(x)
        y.sort()
else:
    ans = list(S)
    for i in alpha:
        if i not in S:
            ans.append(i)
            break
    #print(ans,S)
if ans == S:
    print(-1)
else:   
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = list(input())
alpha = [chr(i) for i in range(97, 97+26)]
if len(S) == 26:
    ans = list(S)
    x = S[-1]
    for i in range(2,26):
        if alpha.index(x) > alpha.index(S[-1*i]):
            ans = S[:-1*i]
            ans.append(x)
            break
else:
    ans = list(S)
    for i in alpha:
        if i not in S:
            ans.append(i)
            break
    #print(ans,S)
if ans == S:
    print(-1)
else:   
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = list(input())
alpha = [chr(i) for i in range(97, 97+26)]
if len(S) == 26:
    ans = S
    x = S[-1]
    for i in range(2,26):
        if alpha.index(x) > alpha.index(S[-1*i]):
            ans = S[:-1*i]
            ans.append(x)
            break
else:
    ans = list(S)
    for i in alpha:
        if i not in S:
            ans.append(i)
            break
    #print(ans,S)
if ans == S:
    print(-1)
else:   
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = list(input())
alpha = [chr(i) for i in range(97, 97+26)]
if len(S) == 26:
    ans = S
    x = S[-1]
    for i in range(2,26):
        if alpha.index(x) > alpha.index(S[-1*i]):
            ans = S[:-1*i]
            ans.append(x)
            break
else:
    ans = S
    for i in alpha:
        if i not in S:
            ans.append(i)
if ans == S:
    print(-1)
else:   
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    # sys.stdin = open("sample.txt")
    S = Scanner.string()
    if len(S) == 26:
        if list(S) == list(reversed(list(string.ascii_lowercase))):
            print(-1)
            return
        i = 0
        for i in reversed(range(25)):
            if S[i] < S[i+1]:
                break
        ans = S[:i]
        ans += sorted(S[i+1:])[0]
        print(ans)
        return
    else:
        T = Counter(S)
        for c in string.ascii_lowercase:
            if T[c] == 0:
                print(S + c)
                return
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input()
ans = list(s)
alpha = [chr(i) for i in range(97, 97+26)]
if s == "".join(alpha) or (len(s) == 26 and s[0] != "a"): print(-1)
elif len(s) == 26:
    for i in range(len(s)):
        if alpha[i] == ans[i]:
            if i == len(s) - 1:
                ans.append(alpha[i+1])
            else: continue
        else:
            ans = alpha[:i]
            ans[-1] = alpha[i]
            break
    print("".join(ans))
else:
    for s_i in alpha:
        if s_i not in ans:
            ans.append(s_i)
            break
    print("".join(ans))

Submission status: Wrong Answer

Solution code:
S = input()
N=len(S)

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

asc=[]
for i in range(N):
    asc.append(ord(S[i]))
#print(asc)

new=""

for i in(range(97,123)):
    if not i in asc:
        new = chr(i)
        break
if new != "":
    print(S+new)
    exit()

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase
s = input()
if s==ascii_lowercase[::-1]:
    ans = -1
elif len(s) != len(ascii_lowercase):
    for c in ascii_lowercase:
        if c not in s:
            ans = s + c
            break
else:
    rev = s[::-1]
    p = ''
    tmp = 0
    for i in range(len(rev)):
        if len(p)==0 or ord(rev[i])>tmp:
            p += rev[i]
            tmp = ord(rev[i])
        else:
            break   
    if len(p) == len(s):
        ans = -1
    else:
        p = p[::-1]
        q = min(p)
        pp = s.replace(p, '')
        ans = pp[:-1] + q
print(ans)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase
s = input()
if s==ascii_lowercase[::-1]:
    ans = -1
elif len(s) != len(ascii_lowercase):
    for c in ascii_lowercase:
        if c not in s:
            ans = s + c
            break
else:
    rev = s[::-1]
    p = ''
    tmp = 0
    for i in range(len(rev)):
        if len(p)==0 or ord(rev[i])>tmp:
            p += rev[i]
            tmp = ord(rev[i])
        else:
            break   
    if len(p) == len(s):
        ans = -1
    else:
        p = p[::-1]
        q = min(p)
        pp = s.replace(p, '')
        ans = pp + q
print(ans)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase
s = input()
if s==ascii_lowercase[::-1]:
    ans = -1
elif len(s) != len(ascii_lowercase):
    for c in ascii_lowercase:
        if c not in s:
            ans = s + c
            break
else:
    rev = s[::-1]
    p = ''
    tmp = 0
    for i in range(len(rev)):
        if len(p)==0 or ord(rev[i])>tmp:
            p += rev[i]
            tmp = ord(rev[i])
        else:
            break   
    if len(p) == len(s):
        ans = -1
    else:
        q = min(p)
        pp = s.replace(p, '')
        ans = pp + q
print(ans)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase

asc = list(ascii_lowercase)
rev_asc = asc[::-1]

s = input()
if s == ascii_lowercase[::-1]:
    ans = -1
elif len(s) != len(ascii_lowercase):
    for c in ascii_lowercase:
        if c not in s:
            ans = s+c
            break
else:
    rev_s = s[::-1]
    tmp_s = ''
    num = 0
    for c in rev_s:
        if (len(tmp_s)==0) or (ord(c)>num):
            tmp_s += c
            num = ord(c)
        else:
            break
    mn = min(list(tmp_s))
    l = len(s) - len(tmp_s)
    ans = s[:l]
    ans = ans[:-1]+mn
print(ans)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase

asc = list(ascii_lowercase)
rev_asc = asc[::-1]

s = input()
if s == ascii_lowercase[::-1]:
    ans = -1
elif len(s) != len(ascii_lowercase):
    for c in ascii_lowercase:
        if c not in s:
            ans = s+c
            break
else:
    rev_s = s[::-1]
    
    tmp_s = ''
    num = 0
    for c in rev_s:
        if (len(tmp_s)==0) or (ord(c)>num):
            tmp_s += c
            num = ord(c)
        else:
            break
    mn = min(list(tmp_s))
    
    l = len(s) - len(tmp_s)
    ans = s[:l]
    ans = ans[:-1]+mn
print(ans)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase

asc = list(ascii_lowercase)
rev_asc = asc[::-1]

s = input()
lst = list(s)
if s == ascii_lowercase[::-1]:
    ans = -1
elif len(s) != len(asc):
    for c in asc:
        if c not in lst:
            ans = s+c
            break
else:
    ans = ''
    tmp = 0
    for c in s:
        num = ord(c)
        if num > tmp:
            ans += c
            tmp = num
    
    rev_ans = ans[::-1]
    for c in rev_ans:
        if c =='z':
            ans = ans[:len(ans)-1]
            continue
        rep = chr(ord(c) + 1)
        ans = ans.replace(c, rep)
        break
print(ans)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase

asc = list(ascii_lowercase)
rev_asc = asc[::-1]

s = input()
lst = list(s)
if s == ascii_lowercase[::-1]:
    ans = -1
elif len(s) != len(asc):
    for c in asc:
        if c not in lst:
            ans = s+c
            break
else:
    ans = ''
    tmp = 0
    for c in s:
        num = ord(c)
        if num > tmp:
            ans += c
            tmp = num
    
    rev_ans = ans[::-1]
    for c in rev_ans:
        if c =='z':
            ans = ans[:len(ans)-1]
            continue
        rep = chr(ord(c) + 1)
        ans = ans.replace(c, rep)
print(ans)

Submission status: Wrong Answer

Solution code:
import string
S = input()
if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        i = len(S) - 1
        while 0 <= i:
            if S[i-1] < S[i]:
                min_val = min(S[i:])
                i -= 1
                break
            i -= 1
        ans = ""
        for x in range(i):
            ans += S[x]
        ans += min_val
        print(ans)

    exit()


# chars_set = set(S)
# for c in string.ascii_lowercase:
#     if c not in chars_set:
#         S += c
#         break
# print(S)

Submission status: Wrong Answer

Solution code:
import string
S = input()
if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        i = len(S) - 1
        while 0 <= i:
            if S[i-1] < S[i]:
                min_val = min(S[i:])
                i -= 1
                break
            i -= 1
        ans = ""
        for x in range(i):
            ans += S[x]
        ans += min_val
        print(ans)

    exit()


chars_set = set(S)
for c in string.ascii_lowercase:
    if c not in chars_set:
        S += c
        break
print(S)

Submission status: Wrong Answer

Solution code:
import string
S = input()
if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        max_idx = None
        max_char = "a"
        for i, s in enumerate(S):
            if max_char < s:
                max_idx = i
                max_char = s
        ans = ""
        for i in range(max_idx):
            ans += S[i]
        ans_set = set(ans)
        for c in string.ascii_lowercase:
            if c not in ans_set:
                ans = ans[:-1] + c
                break
        print(ans)
    exit()


chars_set = set(S)
for c in string.ascii_lowercase:
    if c not in chars_set:
        S += c
        break
print(S)

Submission status: Wrong Answer

Solution code:
S = list(input())

if len(S) < 26:
    W = list('abcdefghijklmnopqrstuvwxyz')
    for s in S:
        W.remove(s)
    S += W[0]
    print(*S, sep='')
else:
    s_last = S[-1]
    idx = 0
    for i in range(24, -1, -1):
        s = S[i]
        if s > s_last:
            s_last = s
        else:
            idx = i
            break
    if idx == 0:
        print(-1)
    else:
        res = []
        for s in S[idx+1:]:
            if s > S[idx]:
                res.append(s)
        res.sort()
        print(*(S[:idx] + [res[0]]), sep='')

Submission status: Wrong Answer

Solution code:
S = list(input())

W = list('abcdefghijklmnopqrstuvwxyz')

if len(S) < 26:
    for s in S:
        W.remove(s)
    S += W[0]
    print(*S, sep='')
elif S == W[::-1]:
    print(-1)
else:
    idx = W.index(S[0])
    W = W[idx:] + W[:idx]
    ans = []
    for s, w in zip(S, W):
        if s == w:
            ans.append(s)
        else:
            ans.pop(-1)
            ans.append(w)
            break
    else:
        ans = [W[1]]
    print(*ans, sep='')

Submission status: Wrong Answer

Solution code:
#coding:utf-8
S = input()
if len(S)<26:
    for i in [chr(i) for i in range(97, 97+26)]:
        if not i in S:
            print (S+i)
            exit()
else:
    mn = 'z'
    for i in range(len(S)-1):
        mn = min(mn,S[-i-1])

        if (S[-i-1] > S[-i-2]):
            print (S[:-i-2]+mn)
            exit()
        else:
            pass
print (-1)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
S = input()
if len(S)<26:
    for i in [chr(i) for i in range(97, 97+26)]:
        if not i in S:
            print (S+i)
            exit()
else:
    mn = 'z'
    for i in range(len(S)-1):

        if (S[-i-1] > S[-i-2]):
            print (S[:-i-2]+mn)
            exit()
        else:
            mn = min(mn,S[-i-1])
print (-1)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase as low
str = input()
flag = 0
for i in low:
	if i not in str:
		print(str + i)
		flag+=1
		break
if(flag != 1):
	print(-1)

Submission status: Wrong Answer

Solution code:
S = list(input())
n = len(S)
W = list('abcdefghijklmnopqrstuvwxyz')*2

ans = []
if S == list('zyxwvutsrqponmlkjihgfedcba'):
    print(-1)
elif n == 26:
    idx = W.index(S[0])
    for i in range(26):
        if S[i] == W[i+idx]:
            ans.append(S[i])
        else:
            ans.pop(-1)
            ans.append(W[i+idx])
            break
    else:
        ans = [W[idx+1]]
    print(*ans, sep='')
else:
    for s in S:
        W.remove(s)
        ans.append(s)
    ans.append(W[0])
    print(*ans, sep='')

Submission status: Wrong Answer

Solution code:
S = list(input())
n = len(S)
W = list('abcdefghijklmnopqrstuvwxyz')*2

ans = []
if S == list('zyxwvutsrqponmlkjihgfedcba'):
    print(-1)
elif n == 26:
    idx = W.index(S[0])
    for i in range(26):
        if S[i] == W[i+idx]:
            ans.append(S[i])
        else:
            ans.pop(-1)
            ans.append(W[i+idx])
            break
    print(*ans, sep='')
else:
    for s in S:
        W.remove(s)
        ans.append(s)
    ans.append(W[0])
    print(*ans, sep='')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    l = [chr(ord("a") + i) for i in range(26)]
    for si in s:
        l.remove(si)
    if l == []:
        s = s[::-1]
        l.append(s[0])
        ans = []
        f = True
        for si in s[1:]:
            if f:
                if min(l) > si:
                    f = False
                l.append(si)
            else:
                ans.append(si)
        l.sort()
        print("".join(ans[::-1]) + l[1])
    else:
        print("".join(s) + l[0])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    l = [chr(ord("a") + i) for i in range(26)]
    for si in s:
        l.remove(si)
    if l == []:
        s = s[::-1]
        l.append(s[0])
        ans = []
        f = True
        for si in s[1:]:
            if f:
                if min(l) > si:
                    f = False
                l.append(si)
                if not f:
                    ans.append(si)
            else:
                ans.append(si)
        print("".join(ans[::-1]) + l[1])
    else:
        print("".join(s) + l[0])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    l = [chr(ord("a") + i) for i in range(26)]
    for si in s:
        l.remove(si)
    if l == []:
        s = s[::-1]
        l.append(s[0])
        ans = []
        f = True
        for si in s[1:]:
            if f:
                
                if min(l) > si:
                    f = False
                l.append(si)
            else:
                ans.append(si)
        print("".join(ans[::-1]) + l[1])
    else:
        print("".join(s) + l[0])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    l = [chr(ord("a") + i) for i in range(26)]
    for si in s:
        l.remove(si)
    if l == []:
        s = s[::-1]
        l.append(s[0])
        ans = []
        f = True
        for si in s[1:]:
            if f:
                
                if min(l) > si:
                    f = False
                l.append(si)
            else:
                ans.append(si)
        l.sort()
        a = l[1]
        l.remove(a)
        print("".join(ans[::-1]) + a)
    else:
        print("".join(s) + l[0])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    l = [chr(ord("a") + i) for i in range(26)]
    for si in s:
        l.remove(si)
    if l == []:
        s = s[::-1]
        l.append(s[0])
        ans = []
        f = True
        for si in s[1:]:
            if f:
                
                if min(l) > si:
                    f = False
                l.append(si)
            else:
                ans.append(si)
        l.sort()
        a = l[1]
        l.remove(a)
        print("".join(ans[::-1]) + a)
    else:
        print("".join(s) + l[0])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    l = [chr(ord("a") + i) for i in range(26)]
    for si in s:
        l.remove(si)
    if l == []:
        s = s[::-1]
        l.append(s[0])
        ans = []
        f = True
        for si in s[1:]:
            if f:
                l.append(si)
                if min(l) > si:
                    f = False
            else:
                ans.append(si)
        l.sort()
        a = l[1]
        l.remove(a)
        print("".join(ans[::-1]) + a)
    else:
        print("".join(s) + l[0])
    return


#main
if __name__ == '__main__':
    solve()