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:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
from collections import deque


def getN():
    return int(input())


def getList():
    return list(map(int, input().split()))


import math

s = input().strip()
if len(s) == 26:
    tmp = ord("a") - 1
    remain = []
    for i, c in enumerate(s[::-1]):
        if tmp < ord(c):
            tmp = ord(c)
            remain.append(c)
        else:
            remain.append(c)
            print(s[:26-i-1] + "".join(sorted(remain)))
            sys.exit()

    print(-1)
    sys.exit()




alph = [0 for i in range(26)]

for c in s:
    print(c)
    print(ord(c) - ord("a"))
    alph[ord(c) - ord("a")] = 1

for i, al in enumerate(alph):
    if al == 0:
        print(s + chr(ord("a") + i))
        sys.exit()


 # zyxwvutsrqponmlkjaihgfedcb

Submission status: Wrong Answer

Solution code:
S = input()

abc = list("abcdefghijklmnopqrstuvwxyz")
# ord('a') = 97
n = len(abc)
check = [0] * n
for s in S:
    check[ord(s) - 97] = 1

if S == ''.join(abc[::-1]):
    print(-1)
    exit()

if sum(check) != n:
    for i in range(n):
        if check[i] == 0:
            S += abc[i]
            break
    print(S)
else:
    check = [0] * n
    for i in range(1, n):
        if S[-i] > S[-i-1]:
            c = i
            check[ord(S[-i]) - 97] = 1
            break
        check[ord(S[-i]) - 97] = 1
    ans = S[:-i-1]
    for i in range(n - 1, ord(S[-i-1]) - 97, -1):
        if check[i] == 1:
            ans += S[i]
            break
    print(ans)

Submission status: Wrong Answer

Solution code:
S = input()

abc = list("abcdefghijklmnopqrstuvwxyz")
# ord('a') = 97
n = len(abc)
check = [0] * n
for s in S:
    check[ord(s) - 97] = 1

if sum(check) != n:
    for i in range(n):
        if check[i] == 0:
            S += abc[i]
            break
    print(S)
else:
    check = [0] * n
    c = n - 1
    for i in range(1, n):
        if S[-i] > S[-i-1]:
            c = i
            check[ord(S[-i]) - 97] = 1
            break
        check[ord(S[-i]) - 97] = 1
    if c == 0:
        ans = -1
    else:
        ans = S[:-i-1]
        for i in range(n - 1, ord(S[-i-1]) - 97, -1):
            if check[i] == 1:
                ans += S[i]
                break
    print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
if len(S) == 26:
    idx = -1
    for i in range(len(S)-1, 0, -1):
        if S[i-1] < S[i]:
            idx = i
            break
    if idx == -1:
        print(-1)
    else:
        if idx != 1:
            print(S[:idx-1]+S[-1])
        else:
            print(chr(ord(S[0])+1))
else:
    for i in range(97, 97+26):
        if chr(i) not in S:
            key = chr(i)
            break
    print(S+key)

Submission status: Wrong Answer

Solution code:
S = input()
if len(S) == 26:
    idx = -1
    for i in range(len(S)-1, 0, -1):
        if S[i-1] < S[i]:
            idx = i
            break
    if idx == -1:
        print(-1)
    else:
        print(S[:idx-1]+S[-1])
else:
    for i in range(97, 97+26):
        if chr(i) not in S:
            key = chr(i)
            break
    print(S+key)

Submission status: Wrong Answer

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

D = [0 for i in range(26)]
for i in range(N):
    D[ord(S[i]) - 97] = 1

if N != 26:
    for i in range(26):
        if D[i] == 0:
            s = chr(97 + i)
            break
    print(S + s)

else:
    if S == "abcdefghijklmnopqrstuvwxyz"[::-1]:
        print(-1)
    else:
        c = 2
        for i in range(N):
            if ord(S[-2 - i]) - ord(S[-1 - i]) >= 1:
                c += 1
            else:
                break
        print(S[:26 - c] + S[-1])

Submission status: Wrong Answer

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

D = [0 for i in range(26)]
for i in range(N):
    D[ord(S[i]) - 97] = 1

if N != 26:
    for i in range(26):
        if D[i] == 0:
            s = chr(97 + i)
            break
    print(S + s)

else:
    if S == "abcdefghijklmnopqrstuvwxyz"[::-1]:
        print(-1)
    else:
        c = 1
        for i in range(N):
            if ord(S[-2 - i]) - ord(S[-1 - i]) == 1:
                c += 1
            else:
                break
        print(S[:26 - c] + S[-1])

Submission status: Wrong Answer

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

D = [0 for i in range(26)]
for i in range(N):
    D[ord(S[i]) - 97] = 1

if N != 26:
    for i in range(26):
        if D[i] == 0:
            s = chr(97 + i)
            break
    print(S + s)

else:
    if S == "abcdefghijklmnopqrstuvwxyz"[::-1]:
        print(-1)
    else:
        c = 2
        for i in range(N):
            if ord(S[-2 - i]) - ord(S[-1 - i]) == 1:
                c += 1
            else:
                break
        print(S[:26 - c] + S[-1])

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = list(input())
    n = len(s)
    alp = list("abcdefghijklmnopqrstuvwxyz")
    f = {}
    for i in range(26):
        f[alp[i]] = i
    if n == 26:
        if s == alp[::-1]:
            print(-1)
        else:
            d = [1]*26
            m = f[s[-1]]
            d[m] = 0
            for i in range(n-1)[::-1]:
                if m > f[s[i]]:
                    break
                m = f[s[i]]
                d[m] = 0
            k = f[s[i]]
            for i in range(k+1,n):
                if not d[i]:
                    break
            s = s[:i-1]+[alp[i]]
            print(*s,sep = "")
    else:
        d = [1]*26
        for i in s:
            d[f[i]] = 0
        for i in range(26):
            if d[i]:
                break
        s += [alp[i]]
        print(*s,sep = "")
    return
#B
def B():

    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = list(input())
    n = len(s)
    alp = list("abcdefghijklmnopqrstuvwxyz")
    f = {}
    for i in range(26):
        f[alp[i]] = i
    if n == 26:
        if s == alp[::-1]:
            print(-1)
        else:
            d = [1]*26
            m = f[s[-1]]
            d[m] = 0
            for i in range(n-1)[::-1]:
                if m > f[s[i]]:
                    break
                m = f[s[i]]
                d[m] = 0
            k = f[s[i]]
            for i in range(k+1,n):
                if not d[i]:
                    break
            s = s[:i]+[alp[i]]
            print(*s,sep = "")
    else:
        d = [1]*26
        for i in s:
            d[f[i]] = 0
        for i in range(26):
            if d[i]:
                break
        s += [alp[i]]
        print(*s,sep = "")
    return
#B
def B():

    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    alpha = "abcdefghijklmnopqrstuvwxyz"
    alpha_rev = alpha[::-1]
    if S == alpha_rev:
        return -1
    Sset = set(list(S))
    if len(Sset) != 26:
        for c in alpha:
            if c not in Sset:
                return S + c
    else:
        for i in range(len(S)-1, 0, -1):
            if S[i] > S[i-1]:
                target = sorted(S[i-1:])
                for c in target:
                    if c > S[i-1]:
                        ref = c
                        break
                t = S[:i-1] + ref + "".join([c for c in target if c != ref])
                return t
        return -1

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
S = input()
alphabet = "abcdefghijklmnopqrstuvxyz"

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

dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(S) < len(alphabet):
    for a in alphabet:
        if dict[a] == 0:
            print(S + a)
            exit()
else:
    ans = ""
    for s,a in zip(S, alphabet):
        print(s,a)
        if s != a:
            print(ans + chr(ord(s)+1))
            exit()
        ans += s

print(-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
S = input()
alphabet= "abcdefghijklmnopqrstuvxyz"

dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(S) < len(alphabet):
    for a in alphabet:
        if dict[a] == 0:
            print(S + a)
            exit()
else:
    ans = ""
    for s,a in zip(S, alphabet):
        if s != a:
            print(ans + chr(ord(s)+1))
            exit()
        ans += s
print(-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
S = input()
alphabet= "abcdefghijklmnopqrstuvxyz"

dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(S) < len(alphabet):
    for a in alphabet:
        if dict[a] == 0:
            print(S + a)
            exit()
else:
    ans = ""
    for s,a in zip(S, alphabet):
        print(s, a)
        if s != a:
            print(ans + chr(ord(s)+1))
            exit()
        ans += s
print(-1)

Submission status: Wrong Answer

Solution code:
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
s=list(s)
al="abcdefghijklmnopqrstuvwxyz"
from collections import Counter
l=Counter(s)
f=0
for i in range(26):
    if l[al[i]]:
        continue
    else:
        s.append(al[i])
        f=1
        break
if f:
    print("".join(s))
else:
    all=[]
    num=[]
    ff=0
    for i in range(1,len(s)):
        if s[i]<s[i-1]:
            ff=1
            all.append(s[i-1])
            num.append(i-1)
            for j in range(i,len(s)):
                all.append(s[j])
            break
    if ff:
        all.sort()
        s[num[0]-1]=all[0]
        print("".join(s[:num[0]]))
    else:
        print("abcdefghijklmnopqrstuvwxz")

Submission status: Wrong Answer

Solution code:
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
s=list(s)
al="abcdefghijklmnopqrstuvwxyz"
from collections import Counter
l=Counter(s)
f=0
for i in range(26):
    if l[al[i]]:
        continue
    else:
        s.append(al[i])
        f=1
        break
if f:
    print("".join(s))
else:
    all=[]
    num=[]
    ff=0
    for i in range(1,len(s)):
        if s[i]!=al[i]:
            ff=1
            all.append(s[i])
            num.append(i)
    if ff:
        all.sort()
        s[num[0]-1]=all[0]
        print("".join(s[:num[0]]))
    else:
        print("abcdefghijklmnopqrstuvwxz")

Submission status: Wrong Answer

Solution code:
S = input()
l = [0] * 27
l[-1] = 1

for i in range(len(S)):
    l[ord(S[i]) - ord('a')] += 1

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

while len(S) > 0:
    if l[ord(S[-1])-ord('a')+1] == 0:
        print(S[:-1] + chr(ord(S[-1])+1))
        exit()
    else:
        l[ord(S[-1])-ord('a')] = 0
        S = S[:-1]

print(-1)

Submission status: Wrong Answer

Solution code:
S = input()
l = [0] * 27

for i in range(len(S)):
    l[ord(S[i]) - ord('a')] += 1

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

while S:
    if l[ord(S[-1])-ord('a')+1] == 0 and S[-1] != 'z':
        print(S[:-1] + chr(ord(S[-1])+1))
        exit()
    else:
        l[ord(S[-1])-ord('a')] = 0
        S = S[:-1]

print(-1)

Submission status: Wrong Answer

Solution code:
S = list(input())

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, sep = '', end = '')
            print (a[i])
            exit()

for i in range(25, 0, -1):
    if ord(S[i]) < ord(S[i - 1]):
        continue
    else:
        break
# print (i)
if i == 1:
    print (-1)
    exit()

tmp_n = ord('z')
for j in S[i:]:
    if ord(j) > ord(S[i - 1]):
        tmp_n = min(tmp_n, ord(j))

print (*S[:i - 1], sep = '', end = '')
print (chr(tmp_n))

Submission status: Wrong Answer

Solution code:
S = list(input())

#最後かどうかのチェック
# final = 'zyxwvutsrqponmlkjihgfedcba'
# flag = True
# for i in range(len(S)):
#     if S[i] == final[i]:
#         pass
#     else:
#         flag = False
#         break

# if flag and len(S) == 26:
#     print (-1)
#     # exit()

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, sep = '', end = '')
            print (a[i])
            exit()

for i in range(25, 0, -1):
    if ord(S[i]) < ord(S[i - 1]):
        continue
    else:
        break
# print (i)
if i == 1:
    print (-1)
    exit()

tmp_n = ord('z')
for j in S[i:]:
    if ord(j) > ord(S[i - 1]):
        tmp_n = min(tmp_n, ord(j))

print (*S[:i - 1], sep = '', end = '')
print (chr(tmp_n))

Submission status: Wrong Answer

Solution code:
S = list(input())

#最後かどうかのチェック
final = 'zyxwvutsrqponmlkjihgfedcba'
flag = True
for i in range(len(S)):
    if S[i] == final[i]:
        pass
    else:
        flag = False
        break

if flag and len(S) == 26:
    print (-1)
    exit()

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, sep = '', end = '')
            print (a[i])
            exit()

#26文字全て使っている時
#後ろから正順になっているところを探す

# for i in range(25, 0, -1):
#     if ord(S[i]) - ord(S[i - 1]) > 0:
#         break
# ans = S[:i]
# a = 'abcdefghijklmnopqrstuvwxyz'
# if len(ans) != 26:
#     for i in range(26):
#         if not a[i] in ans:
#             print (*ans[:-1], sep = '', end = '')
#             print (a[i])
#             exit()

Submission status: Wrong Answer

Solution code:
S = list(input())

#最後かどうかのチェック
final = 'zyxwvutsrqponmlkjihgfedcba'
flag = True
for i in range(len(S)):
    if S[i] == final[i]:
        pass
    else:
        flag = False
        break

if flag and len(S) == 26:
    print (-1)
    exit()

a = 'abcdefghijklmnopqrstuvwxyz'
if len(S) != 26:
    for i in range(26):
        if not a[i] in S:
            print (*S, sep = '', end = '')
            print (a[i])
            exit()

#26文字全て使っている時
#後ろから正順になっているところを探す
for i in range(25, 0, -1):
    if ord(S[i]) - ord(S[i - 1]) > 0:
        break
ans = S[:i]
a = 'abcdefghijklmnopqrstuvwxyz'
if len(ans) != 26:
    for i in range(26):
        if not a[i] in ans:
            print (*ans[:-1], sep = '', end = '')
            print (a[i])
            exit()

Submission status: Wrong Answer

Solution code:
d=list("abcdefghijklmnopqrstuvwxyz")
s=input()
ss=list(s)
unused=list(set(d)^set(ss))
unused.sort()
if len(s)==26:
    cnt=1
    l=d.index(s[-1])
    for i in range(2,27):
        if d.index(s[-i])>l:
            l=d.index(s[-i])
            cnt+=1
        else:
            ans=ss[:26-cnt-1]+[s[-1]]
            print("".join(ans))
            exit()
    print(-1)
else:
    ans=ss+[unused[0]]
    print("".join(ans))

Submission status: Wrong Answer

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

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

alpha = 'abcdefghijklmnopqrstuvwxyz'
s = input()
if s == alpha[::-1]:
    print(-1)
    quit()
if len(s) < 26:
    for i in alpha:
        if i not in s:
            print(s+i)
            break
else:
    tmp = []
    for i in range(1,len(s))[::-1]:
        if s[i-1] > s[i]:
            tmp.append(s[i])
        else:
            tmp.append(s[i])
            break
    for j in tmp:
        if s[i-2] < j:
            print(s[:i-1] + j)
            break

Submission status: Wrong Answer

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

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

alpha = 'abcdefghijklmnopqrstuvwxyz'
s = input()
if s == alpha[::-1]:
    print(-1)
    quit()
if len(s) < 26:
    for i in alpha:
        if i not in s:
            print(s+i)
            break
else:
    for i in range(1,len(s))[::-1]:
        if s[i] < s[i-1]:
            print(s[:i-3] + s[i])
            break

Submission status: Wrong Answer

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

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

alpha = 'abcdefghijklmnopqrstuvwxyz'
s = input()
t = list(Counter(s).items())
f = defaultdict(lambda : 0)
for i, j in t:
    f[i] += 1
if len(s) < 26:
    ans = s
    for i in alpha:
        if f[i] == 0:
            ans += i
            break
    print(ans)
else:
    for i in range(len(s)-1):
        if s[i] > s[i+1]:
            if i == 0:
                print(-1)
                quit()
            break
    t = s[i:]
    tmp = s[i-1]
    ans = s[:i-1]
    m = 'z'
    for i in range(len(t)):
        if ord(tmp) < ord(t[i]):
            if ord(m) > ord(t[i]):
                m = t[i]
    ans += m
    print(ans)

Submission status: Wrong Answer

Solution code:
#%%
alpha2num = lambda c: ord(c) - ord('a') + 1
num2alpha = lambda c: chr(c+64)
S = list(input())

S = S[::-1]
alpha = 'abcdefghijklmnopqrstuvwxyz'
alpha = list(alpha)

a = 0

if len(S) != 26:
    for i in range(len(alpha)):
        if alpha[i] not in S:
            b = alpha[i]
            break
    ans = ''.join(S)[::-1] + b
else:
    for i in range(1,len(S)):
        if S[i] < S[i-1]:
            a = i
            break
    if a == 0:
        ans = -1
    else:
        ans = (num2alpha(alpha2num(S[a])+1).lower() + ''.join(S[i+1:]))[::-1]
print(ans)


#%%

Submission status: Wrong Answer

Solution code:
# 2019/08/12

s=input()
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

st=set(s)

if len(s)<26:
    for i in range(97,97+26+1):
        if chr(i) in st:continue
        print(s+chr(i))
        exit()

s=list(s)
res=[s.pop()]

def solve(s):
    for _ in range(len(s)):
        tmp=s.pop()
        if tmp>res[-1]:
            res.append(tmp)
            res.sort()
        else:            
            s.append(min(res))
            return s

print(''.join(solve(s)))

Submission status: Wrong Answer

Solution code:
S0="zyxwvutsrqponmlkjihgfedcba"
S=input()
if S==S0:
    print(-1)
elif len(S) == 26:
    for i in reversed(range(len(S)-1)):
        if S[i]<S[i+1]:
            print(S[:i]+S[-1])
            exit()
else:
    for c in sorted(S0):
        if c not in S:
            print(S+c)
            exit()

Submission status: Wrong Answer

Solution code:
S=list(input())
l=len(S)

available=[]
for i in range(26):
    available.append(chr(i+97))

for i in S:
    available.remove(i)

if l<26:
    S.append(available[0])
else:
    for i in range(l):
        want=chr(ord(S[-1])+1)
        if want in available:
            S.remove(S[-1])
            S.append(want)
            break
        else:
            available.append(S[-1])
            S.remove(S[-1])
if len(S)==0:
    ans='-1'
else:
    ans=''
    for i in S:
        ans+=i
print(ans)

Submission status: Wrong Answer

Solution code:
# AGC022A - Diverse Word
from string import ascii_lowercase


def main():
    S = input().rstrip()
    A = set(ascii_lowercase)
    if len(S) < 26:
        R = A - set(S)  # remaining
        print(S + sorted(R)[0])  # S + the first letter in R
    else:
        for i in range(25, 0, -1):
            P = S[i:][::-1]  # partial string
            SP = sorted(P)
            if P != "".join(SP):
                print(S[:i] + SP[1])
                break
        else:  # the most diverse word
            print(-1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def nextchr(c):
    return chr(ord(c) + 1)

def trim(s): # 重複文字をなくす
    n = len(s)
    for i in range(n):
        if s[i] in s[:i]:
            if s[i] == "z":
                return trim(s[:i-1] + nextchr(s[i-1]))
            else:
                return trim(s[:i] + nextchr(s[i]))
    return s

def solve(s):
    t = trim(s)
    if s < t:
        return t
    if len(s) < 26: # なにかの文字を末尾に追加できる
        for i in range(ord("a"), ord("z")+1):
            c = chr(i)
            if not c in s:
                return s + c
    # 繰り上げが存在
    n = len(s)
    i = n-2
    while (i >= 0) and (s[i] > s[n-1]):
        i -= 1
    if i < 0:
        return -1
    else:
        return s[:i] + s[n-1]

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

Submission status: Wrong Answer

Solution code:
S = input()

N = len(S)
alpha = [chr(i) for i in range(97, 97+26)]
flag = [False for i in range(26)]

if N != 26:
    for i in range(N):
        flag[ord(S[i])-97] = True
    for i in range(26):    
        if flag[i] == False:
            S += chr(i+97)
            break
    print(S)
    exit()

num = 0
for i in range(1, N)[::-1]:
    if S[i-1] < S[i]:
        num = i
        break
if num == 0:
    print(-1)
    exit()

for i in range(num):
    flag[ord(S[i])-97] = True
S = S[:num-1]

for i in range(26):    
    if flag[i] == False:
        S += chr(i+97)
        break
print(S)

Submission status: Wrong Answer

Solution code:
S = input()

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

if N != 26:
    flag = [False for i in range(26)]
    for i in range(N):
        flag[ord(S[i])-97] = True
    for i in range(26):    
        if flag[i] == False:
            S += chr(i+97)
            break
    print(S)
else:
    flag = [False for i in range(26)]
    num = 0
    for i in range(N-1)[::-1]:
        if S[i] > S[i-1]:
            num = i
            break
    if num == 0:
        print(-1)
        exit()

    for i in range(num):
        flag[ord(S[i])-97] = True
    S = S[:num-1]
    
    for i in range(26):    
        if flag[i] == False:
            S += chr(i+97)
            break
    print(S)

Submission status: Wrong Answer

Solution code:
S = input()

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

if N != 26:
    flag = [False for i in range(26)]
    for i in range(N):
        flag[ord(S[i])-97] = True
    for i in range(26):    
        if flag[i] == False:
            S += chr(i+97)
            break
    print(S)
else:
    gyaku = [chr(i) for i in range(97, 97+26)]
    gyaku.reverse()
    gyaku = "".join(gyaku)
    if gyaku == S:
        print(-1)
    else:
        flag = [False for i in range(26)]
        for i in range(N-1):
            if S[i] > S[i+1]:
                S = S[:i-1]
                break
            flag[ord(S[i])-97] = True
        for i in range(26):    
            if flag[i] == False:
                S += chr(i+97)
                break
        print(S)

Submission status: Wrong Answer

Solution code:
s = input()
t = 'abcdefghijklmnopqrstuvwxyz'

if len(s) == len(t):
  for i in range(24,-1,-1):
    if s[25] > s[i]:
      print(s[:i]+s[25])
      exit()
  print(-1)
  exit()
else:
  for i in t:
    if i not in s:
      print(s+i)
      exit()

Submission status: Wrong Answer

Solution code:
S = input()

alpha2num = lambda c: ord(c) - ord('a') + 1
num2alpha = lambda c: chr(c+96)

snum = list(map(alpha2num, list(S)))

def next_snum(snum):
    if len(snum) <26:
        for i in range(1,27):
            if not i in snum:
                snum.append(i)
                return snum
    else:
        for i in range(25,0,-1):
#             print("hoge")
            # 後ろから見ていき、左より右が大きいことがあれば、
            if snum[i-1] < snum[i]:
                # i-1番目は消す。i番目以降で、i-1より大きく、もっとも小さい数を足す
                # snum[i]は必ず該当するので、そのようなminnumが1つ以上存在する
                minnum = 27
                for nxt in snum[i:]:
                    if minnum > nxt and nxt > snum[i-1]:
                        minnum = nxt
                        
#                 print("nxt:",nxt)
                snum = snum[:i-2] + [minnum]
#                 print(snum)
                return snum
        # 1つも上記のようなものがなければ、次のSは存在しない
        return -1

result_num = next_snum(snum)

if result_num == -1:
    print(-1)
else:
    print("".join(list(map(num2alpha,result_num))))

Submission status: Wrong Answer

Solution code:
S = input()

alpha2num = lambda c: ord(c) - ord('a') + 1
num2alpha = lambda c: chr(c+96)

snum = list(map(alpha2num, list(S)))

def next_snum(snum):
    if len(snum) <26:
        for i in range(1,27):
            if not i in snum:
                snum.append(i)
                return snum
    else:
        for i in range(25,0,-1):
            print("hoge")
            # 後ろから見ていき、左より右が大きいことがあれば、
            if snum[i-1] < snum[i]:
                # i-1番目は消す。i番目以降で、i-1より大きく、もっとも小さい数を足す
                # snum[i]は必ず該当するので、そのようなminnumが1つ以上存在する
                minnum = 27
                for nxt in snum[i:]:
                    if minnum > nxt and nxt > snum[i-1]:
                        minnum = nxt
                        
#                 print("nxt:",nxt)
                snum = snum[:i-2] + [minnum]
#                 print(snum)
                return snum
        # 1つも上記のようなものがなければ、次のSは存在しない
        return -1

result_num = next_snum(snum)

if result_num == -1:
    print(-1)
else:
    print("".join(list(map(num2alpha,result_num))))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import string

S = (stdin.readline().rstrip())
remain = string.ascii_lowercase

if S == remain[::-1]:
    print(-1)

else:

    for i in S:
        remain = remain.replace(i, "")

    if remain != "":
        ans = S + sorted(remain)[0]
        print(ans)

    else:
        for i in range(25, 0, -1):
            if S[:i][-1] < sorted(S[i:])[0]:
                ans = S[:i-1] + sorted(S[i:])[0]
                print(ans)
                break

Submission status: Wrong Answer

Solution code:
from sys import stdin
import string

S = (stdin.readline().rstrip())
remain = string.ascii_lowercase

if S == remain[::-1]:
    print(-1)

else:
    
    for i in S:
        remain = remain.replace(i, "")

    if remain != "":
        ans = S + sorted(remain)[0]
        print(ans)
    
    else:
        for l,(j,k) in enumerate(zip(S,string.ascii_lowercase)):
            if j != k:
                ans = S[:l+1] + sorted(S[l+1:])[0]
                print(ans)
                break

Submission status: Wrong Answer

Solution code:
from sys import stdin
import string

S = (stdin.readline().rstrip())
remain = string.ascii_lowercase

if S == remain[::-1]:
    print(-1)

else:
    
    for i in S:
        remain = remain.replace(i, "")

    if remain != "":
        ans = S + sorted(remain)[0]
        print(ans)
    
    else:
        for l,(j,k) in enumerate(zip(S,string.ascii_lowercase)):
            if j != k:
                ans = S[:l-1] + sorted(S[l:])[0]
                print(ans)
                break

Submission status: Wrong Answer

Solution code:
from sys import stdin
import string

S = (stdin.readline().rstrip())
remain = string.ascii_lowercase

if S == remain[::-1]:
    print(-1)

else:
    
    for i in S:
        remain = remain.replace(i, "")

    if remain != "":
        ans = S + remain[0]
        print(ans)
    
    else:
        for l,(j,k) in enumerate(zip(S,string.ascii_lowercase)):
            if j != k:
                ans = S[:l-1] + sorted(S[l:])[0]
                print(ans)
                break

Submission status: Wrong Answer

Solution code:
from sys import stdin
import string

S = (stdin.readline().rstrip())
remain = string.ascii_lowercase

if S == remain[::-1]:
    print(-1)

else:
    
    for i in S:
        remain = remain.replace(i, "")

    if remain != "":
        ans = S + remain[0]
        print(ans)
    
    else:
        for l,(j,k) in enumerate(zip(S,string.ascii_lowercase)):
            if j != k:
                ans = S[:l] + sorted(S[l:])[0]
                print(ans)
                break