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:
from string import*;from itertools import*;b=ascii_lowercase;s=r=input();t=s[::-1];d=0
if len(s)>25:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(t,t[1:])])][0]-2;s,b,r=s[:d],s[d+1:],b[:ord((s*2)[d])-97]
print(-(d<-26)or s+sorted(set(b)-set(r))[0])

Submission status: Accepted

Solution code:
from string import*;from itertools import*;b=ascii_lowercase;s=r=input();t=s[::-1];d=0
if len(s)>25:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(t,t[1:])])][0]-2;u=s*2;s,b,r=s[:d],s[d+1:],b[:ord(u[d])-97]
print(-(d<-26)or s+sorted(set(b)-set(r))[0])

Submission status: Accepted

Solution code:
A=input()
l=set(chr(ord('a') + i) for i in range(26))
l2=list(l-set(A))
l2.sort()
if len(A)!=26:
   print(A+l2[0])
else:
   for i in reversed(range(25)):
      for j in reversed(range(i+1,26)):
         if A[i] < A[j]:
            print(A[:i]+A[j])
            exit()
   else:
      print(-1)

Submission status: Accepted

Solution code:
from string import*;from itertools import*;b=ascii_lowercase;s=input();t=s[::-1]
if len(s)<26:print(s +sorted(set(b)-set(s))[0])
else:d=-[p*len(list(k))for p,k in groupby([i<j for i,j in zip(t,t[1:])])][0]-2;print(-(d<-26)or s[:d]+sorted(set(s[d+1:])-set(b[:ord(s[d])-97]))[0])

Submission status: Accepted

Solution code:
from string import*
from itertools import*
b = ascii_lowercase
s = input()
if len(s) < 26:
    print(s + sorted(set(b) - set(s))[0])
else:
    d = -[p*len(list(k)) for p, k in groupby([i < j for i, j in zip(s[-1::-1], s[-2::-1])])][0] - 2
    print(-(d < -26) or s[:d] + sorted(set(s[d+1:]) - set(b[:ord(s[d]) - 97]))[0])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from string import*
from itertools import*
b = ascii_lowercase
s = input()
l = len(s)
if l < 26:
    print(s + sorted(set(b) - set(s))[0])
else:
    t = s[::-1]
    a = [(p, len(list(k))) for p, k in groupby([i < j for i, j in zip(t, t[1:])])][0]
    d = - a[0]*a[1] - 2
    print(-(d < -26) or s[:d] + sorted(set(s[d+1:]) - set(b[:ord(s[d]) - 97]))[0])

Submission status: Accepted

Solution code:
s = input()
word = [0] * 26
for i in s:
    word[ord(i) - ord('a')] = 1

if sum(word) != 26:
    for i in range(26):
        if word[i] == 0:
            c = chr(i + ord('a'))
            break
    print(s + c)
    exit()
else:
    for i in range(25, -1, -1):
        if s[i] == 'z':
            pass
        else:
            now = ord(s[i]) + 1
            for j in range(now, ord('z') + 1):
                if not chr(j) in s[0:i]:
                    print(s[0:i] + chr(j))
                    exit()
print(-1)

Submission status: Accepted

Solution code:
s = input()
word = [0] * 26
for i in s:
    word[ord(i) - ord('a')] = 1

if sum(word) != 26:
    for i in range(26):
        if word[i] == 0:
            c = chr(i + ord('a'))
            break
    print(s + c)
else:
    if s == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        for i in range(25, -1, -1):
            if s[i] == 'z':
                pass
            else:
                now = ord(s[i]) + 1
                for j in range(now, ord('z')+1):
                    if chr(j) in s[0:i]:
                        pass
                    else:
                        print(s[0:i] + chr(j))
                        exit()

Submission status: Accepted

Solution code:
s = input()
if len(set(s))==26:
    for i in range(26):
        for j in range(i+1):
            if s[-(i+1)]<s[-(j+1)]:
                print(s[:-(i+1)]+s[-(j+1)]);exit()
    else:
        print(-1);exit()
else:
    chr_li = [ord(chr_) for chr_ in s]
    for i in range(97,97+26):
        if i not in chr_li:
            print(s+chr(i));exit()

Submission status: Accepted

Solution code:
s = input()
l = len(s)

alf=set('abcdefghijklmnopqrstuvwxyz')
for i in range(l):
    if s[i] in alf:
        alf.remove(s[i])

if alf:
    t = list(alf)
    t.sort()
    s+=t[0]
else:
    stock=[]
    for i in range(1,26):
        if s[l-i-1]>s[l-i]:
            stock.append(s[l-i])
        else:
            stock.append(s[l-i])
            stock.sort()
            for j in stock:
                if j>s[l-i-1]:
                    s=s[:l-i-1]+j
                    print(s)
                    exit()
        if i==25:
            print('-1')
            exit()
print(s)

Submission status: Accepted

Solution code:
s=input()
t="abcdefghijklmnopqrstuvwxyz"
if s==t[::-1]:
  print(-1)
  exit(0)
if len(s)<26:
  for ti in t:
    if not ti in s:
      print(s+ti)
      exit(0)
else:
  for i in range(25):
    if s[-i-2]<s[-1-i]:
      t=s[:-i-2]
      u=sorted(list(s[-i-2:]))
      print(t+u[u.index(s[-2-i])+1])
      exit(0)

Submission status: Accepted

Solution code:
s = input()
# print(s[:-1])
if len(s) != 26:
    for c in range(ord('a'), ord('z')+1):
        c = chr(c)
        if not c in s:
            print(s+c)
            exit()
s = list(s)
t = []
while s:
    for c in t:
        if c > s[-1]:
            print(''.join(s[:-1])+c)
            exit()
    t.append(s.pop())

print('-1')

Submission status: Accepted

Solution code:
s=input()
if len(s)<26:
  for i in range(97,123):
    if not chr(i) in s:
      print(s+chr(i))
      exit()
else:
  for i in range(25)[::-1]:
    for j in range(i+1,26)[::-1]:
      if s[i]<s[j]:
        print(s[:i]+s[j])
        exit()
  else:
    print(-1)

Submission status: Accepted

Solution code:
S = input()
s = list(S)
o = [ord(x) for x in S]
alphabets = set([chr(i+97) for i in range(26)])
if len(S) < 26:
    used = set(s)
    unused = alphabets - used
    unusedo = [ord(x) for x in unused]
    print(''.join(S)+chr(min(unusedo)))
else:
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print('-1')
    else:
        diff = [ord(s[i+1]) - ord(s[i]) for i in range(25)]
        for i in range(1,26):
            if diff[-i] > 0:
                removed = S[:-i-1]
                current = S[-i-1]
                break
        usedr = set(list(removed)) | set(current)
        unusedr = alphabets - usedr
        unusedro = [ord(x) for x in unusedr if ord(x) > ord(current)]
        print(removed+chr(min(unusedro)))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = readline().rstrip().decode() + chr(96)
for i, ss in enumerate(s[::-1]):
    for m in range(ord(ss) + 1, 123):
        if chr(m) not in list(s[:-(i + 1)]):
            print(s[:-(i + 1)] + chr(m))
            exit()
print(-1)

Submission status: Accepted

Solution code:
import sys
s = input()
dic = "abcdefghijklmnopqrstuvwxyz"
for i in range(len(s),0,-1):
    for j in dic:
        if not j in s[:i]:
            if i == len(s):
                print(s[:i]+j)
                sys.exit()
            else:
                if dic.index(s[i]) < dic.index(j):
                    print(s[:i]+j)
                    sys.exit()
else:
    if s[0] != "z":
        print(dic[dic.index(s[0])+1])
    else:
        print(-1)

Submission status: Accepted

Solution code:
S = input()
alphabet = "abcdefghijklmnopqrstuvwxyz"
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    if len(S)<26:
        for a in alphabet:
            if a not in S:
                print(S + a)
                exit()
    else:#コイツを下回らない程度に辞書順を最低にする
        ordmax = 0
        for n,s in enumerate(reversed(S)):
            if ordmax > ord(s):

                for j in range(alphabet.index(s)+1,26):
                    if alphabet[j] not in S[:-n-1] :
                        print(S[:-n-1] + alphabet[j])
                        exit()
            else:
                ordmax = max(ordmax,ord(s))

Submission status: Accepted

Solution code:
s = input()
alp = "abcdefghijklmnopqrstuvwxyz"
if len(s) == 26:
    bck = []
    for i in range(25):
        bck.append(s[25-i])
        if s[25-i] > s[24-i]:
            for j in range(alp.find(s[24-i]),26):
                if alp[j] in bck:
                    sn = alp[j]
                    break
            print(s[0:24-i]+sn)
            exit()
    print(-1)
else:
    for i in alp:
        if i in s:
            continue
        else:
            print(s+i)
            exit()

Submission status: Accepted

Solution code:
s = input() #sys.stdin.readlineは最後が改行
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

s=list(s)
oa = ord('a')
alp = [True]*26
for i in range(len(s)):
    alp[ord(s[i])-oa]=False

if len(s)<26:
    nokori_min =alp.index(True)
    add = chr(nokori_min + ord('a'))
    s+=add

    print(*s,sep='')
    exit()

nokori = []


from bisect import bisect_right

for i in reversed(range(len(s))):
    if ord(s[i-1]) > ord(s[i]):
        nokori.append(s.pop())
    else:
        if nokori == []:
            tmp = s.pop()
            s[-1] = tmp
            break
        else:

            nokori.append(s.pop())
            nokori.sort()
            index = bisect_right(nokori, s[-1])
            s[-1] = nokori[index]
            break

print(*s,sep='')

Submission status: Accepted

Solution code:
# A - Diverse Word
import numpy as np

S = input()
alphabet = np.zeros(26, dtype=int)
for i in S:
    alphabet[ord(i)-97]=1
if len(S)<26:
    add = alphabet.argmin()
    ans = S + chr(add+97)
else:
    i = 24
    while(i>=0 and S[i]>S[i+1]):
        i -= 1
    if i==-1:
        ans = '-1'
    else:
        j = 25
        while(i+1<j and S[i]>S[j]):
            j -= 1
        ans = S[:i] + S[j]

print(ans)

Submission status: Accepted

Solution code:
S = input()
li = [chr(ord('a') + i) for i in range(26)]
used = [0] * 26
dic = dict(zip(li, used))
for i in range(len(S)): dic[S[i]] = 1
if(len(S) < 26):
    for i in range(26):
        k = chr(ord('a') + i)
        v = dic[k]
        if(v == 0):
            print(S + k)
            exit(0)

if(len(S) == 26):
    for i in range(25, -1, -1):
        for j in range(26): dic[chr(ord('a') + j)] = 0
        for j in range(i): dic[S[j]] = 1
        add_c = ""
        for j in range(i + 1, 26):
            if(S[j] > S[i]):
                if(add_c == ""): add_c = S[j]
                else: add_c = min(add_c, S[j])
        if(add_c):
            print(S[:i] + add_c)
            exit(0)
print(-1)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    s = input()
    if len(s) < 26:
        al = [chr(ord('a') + i) for i in range(26)]
        for i in al:
            if not (i in s):
                print(s + i)
                exit()
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        exit()
    for i in range(25):
        if s[i] < s[i + 1]:
            k = i
    a = "z"
    for i in range(k + 1, 26):
        if s[k] < s[i]:
            a = min(a, s[i])
    print(s[:k] + a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
t = set(list(s))
n = len(s)
al={chr(ord('a') + i) for i in range(26)}
if n != 26:
    p = al - t
    p = list(p)
    p.sort()
    print(s + p[0])
elif n == 26:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        now = s[25]
        p = []
        for i in reversed(range(26)):
            if ord(now) <= ord(s[i]):
                p.append(now)
                now = s[i]
            elif ord(now) > ord(s[i]):
                p.append(now)
                print(s[:i],end="")
                q = []
                for j in p:
                    if ord(s[i]) < ord(j):
                        q.append(ord(j))
                print(chr(min(q)))
                exit()

Submission status: Accepted

Solution code:
from heapq import heappush,heappop

S = input()
slist = "abcdefghijklmnopqrstuvwxyz"

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

lis =[]
T = S
if len(S)<26:
    Sset = set(S)
    for s in slist:
        if s not in Sset:
            print(S+s)
            exit()
else:
    while len(S)>0:
        lis.append(S[-1])
        lis.sort()
        S = S[:-1]
        for s in lis:
            if S + s > T:
                print(S + s)
                exit()

Submission status: Accepted

Solution code:
import collections
s = list(input())
sset = set(s)
if len(sset) != 26:
    for i in range(97,123):
        if chr(i) in sset:
            continue
        else:
            tmp = chr(i)
            s.append(tmp)
            break

    print(''.join(s))
    exit()
else:
    tmp = []
    flg = False
    # 昇順のインデックス見つける
    for i in range(1,26):
        if s[i-1] < s[i]:
            tmp.append(i-1)
            continue
    # ０なら-1
    if len(tmp) == 0:
        print(-1)
        exit()
    # 末尾の昇順のとこかえる
    change_i = max(tmp)
    ans = s[:change_i]
    for i in range(97,123):
        if chr(i) in ans:
            continue
        else:
            if ord(s[change_i]) < i:
                ans.append(chr(i))
                break
    print(''.join(ans))

Submission status: Accepted

Solution code:
import collections
s = list(input())
sset = set(s)
if len(sset) != 26:
    for i in range(97,123):
        if chr(i) in sset:
            continue
        else:
            tmp = chr(i)
            s.append(tmp)
            break

    print(''.join(s))
    exit()
else:
    tmp = []
    flg = False
    for i in range(1,26):
        if s[i-1] < s[i]:
            tmp.append(i-1)
            continue

    if len(tmp) == 0:
        print(-1)
        exit()
    change_i = max(tmp)
    if change_i == 0:
        print(chr(ord(s[0])+1))
        exit()
    ans = s[:change_i]
    for i in range(97,123):
        if chr(i) in ans:
            continue
        else:
            if ord(s[change_i]) < i:
                ans.append(chr(i))
                break
    print(''.join(ans))

Submission status: Accepted

Solution code:
s = input()
a = [0 for i in range(26)]
for i in s:
	b = ord(i) - ord("a")
	a[b] += 1
for i in range(26):
	if a[i] == 0:
		x = chr(ord("a")+i)
		s += x
		print (s)
		exit()
a = [0 for i in range(26)]
for i,j in enumerate(reversed(s)):
	b = ord(j) - ord("a")
	a[b] += 1
	if i == 0: continue
	for k in range(b+1,26):
		if a[k] == 1:
			x = chr(ord("a")+k)
			ans = s[:(26-i)-1] + x
			print (ans)
			exit()
print (-1)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
# import itertools
# import math

S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    sys.exit()
a = [chr(i) for i in range(97, 97+26)]
b = [0 for i in range(26)]
d = dict(zip(a, b))
d1 = d
for i, v in enumerate(S):
    d[v] = d.get(v, 0)+1
for i, v in enumerate(a):
    if d[v] != 1:
        print(S+v)
        sys.exit()
temp = []
temp1 = []

for i in range(25):
    temp1 = []
    temp.append(S[-1-i])
    # print(S[-1-i], S[-1-i-1])
    for k, v in enumerate(temp):
        # print("v="+v)
        # print(S[-1-i-1], ord(S[-1-i-1]), ord(v))
        if S[-1-i-1] < v:
            temp1.append(v)
    # print(temp, temp1)
    if len(temp1) > 0:
        temp1.sort()
        print(S[:-1-i-1]+temp1[0])
        sys.exit()

Submission status: Accepted

Solution code:
S=input()
if len(S)<26:
    print(S+sorted(list({chr(i+97) for i in range(26)}-set(S)))[0])
else:
    tmp = [S[25]]
    for i in range(25):
        tmp.append(S[24-i])
        if S[24-i] < S[25-i]:
            tmp =sorted(tmp)
            print(S[:26-len(tmp)]+tmp[tmp.index(S[24-i])+1])
            exit()
    print(-1)

Submission status: Accepted

Solution code:
def main():
    s = input()
    al = list("abcdefghijklmnopqrstuvwxyz")
    d = set(s)
    n = len(s)
    if list(s) == al[::-1]:
        print(-1)
        return 0
    if n == 26:
        for i in (range(n-1))[::-1]:
            if s[i] < s[i+1]:
                for j in range(i+1,n)[::-1]:
                    if s[i] < s[j]:
                        print(s[:i]+s[j])
                        return 0
    else:
        for i in al:
            if i not in d:
                print(''.join(s)+i)
                return 0

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
if len(s)<26:
    al=[chr(ord('a') + i) for i in range(26)]
    for i in al:
        if not(i in s):
            print(s+i)
            exit()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
a=[]
for i in range(25,-1,-1):
    for j in range(24-(25-i),-1,-1):
        if s[j]<s[i]:
            a.append(s[:j]+s[i])
print(min(a))

Submission status: Accepted

Solution code:
s=input()
from bisect import bisect_left as bl,bisect_right as br
if len(s)==26:
  if s=="zyxwvutsrqponmlkjihgfedcba":print(-1);exit()
  f=s[-1]
  p=[s[-1]]
  for i in range(2,26):
    if s[-i]>f:
      f=s[-i];p.append(f)
    else:print(s[:-i]+p[bl(p,s[-i])]);exit()
  print(chr(ord(s[0])+1))
else:
  f=sorted(list(set(list("aqzxswedcvfrtgbnhyujmkilop"))-set(list(s))))
  print(s+f[0])

Submission status: Accepted

Solution code:
s = input()
if len(s)<26:
    seen = set()
    for c in s:
        seen.add(c)
    for i in range(26):
        tmp = chr(i+ord('a'))
        if tmp not in seen:
            print(s+tmp)
            break
else:
    pre = 'a'
    seen = set()
    for i, c in enumerate(reversed(s)):
        if c < pre:
            for j in range(26):
                tmp = chr(j+ord(c)+1)
                if tmp in seen:
                    print(s[:25-i]+tmp)
                    break
            break
        seen.add(c)
        pre = c
    else:
        print(-1)

Submission status: Accepted

Solution code:
import string
ABC=list(string.ascii_lowercase)
s=input()
S=list(s[::-1])

if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    if len(s)==26:
        for i in range(len(s)):
            n=ABC.index(S[i])
            if not set(ABC[n+1:])<=set(S[i+1:]):
                j=i
                break
        L=list(set(ABC[n+1:])-set(S[j+1:]))
        L.sort()
        sgyku=L[0:1]+S[j+1:]
        print("".join(sgyku[::-1]))
    else:
        L=list(set(ABC)-set(s))
        L.sort()
        print(s+L[0])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    S = list(input())


    if len(S) == 26:
        # abc = [chr(i) for i in range(97, 123)]
        # print(S)
        for i in range(25, -1, -1):
            for j in range(25, i, -1):
                # print(i, j, S[i], S[j])
                if S[i] < S[j]:
                    ans = S[:i] + [S[j]]
                    printlist(ans, '')
                    return

        print(-1)

    else:
        abc = [chr(i) for i in range(97, 123)]

        for s in S:
            if s in abc:
                abc[ord(s) - 97] = 0

        for t in abc:
            if t != 0:
                S.append(t)
                printlist(S, k='')
                return


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    S = list(input())

    abc = [chr(i) for i in range(97, 123)]
    # print(abc)

    for s in S:
        if s in abc:
            abc[ord(s) - 97] = 0



    if len(S) == 26:
        # abc = [chr(i) for i in range(97, 123)]
        # print(S)
        SR = S[::-1]
        # print(SR)
        idx = (INF, INF)
        for i in range(26):
            for j in range(i+1, 26):
                if SR[i] > SR[j]:
                    # print(i, j)
                    # print(S[:-j])
                    if j < idx[1]:
                        idx = (i, j)
                    break
        # print(idx)
        i, j = idx
        if i == INF:
            print(-1)
        else:
            ans = S[:-j-1]
            ans.append(SR[i])
            printlist(ans, '')

    else:
        for t in abc:
            if t != 0:
                S.append(t)
                printlist(S, k='')
                return

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import string
def main():
    s = input()
    if len(s) < 26:
        print(s + sorted(list(set(string.ascii_lowercase) - set(s)))[0])
    else:
        t = []
        for i in range(len(s)):
            te = []
            for j in range(i+1, len(s)):
                if s[i] < s[j]: te.append(s[j])
            te.sort()
            t.append(te)
        for i in range(len(s)):
            if t[-1-i]:
                print(s[:-1-i] + t[-1-i][0])
                break
        else: print(-1)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

s = list(input())
n = len(s)
d = list('abcdefghijklmnopqrstuvwxyz')
se = set(s)
if n < 26:
    for i,x in enumerate(d):
        if not x in se:
            s.append(x)
            break
    print(''.join(s))
else:
    for i in range(25)[::-1]:
        if s[i] < s[i+1]:
            tmp = s[i+1:n]
            tmp.sort()
            for j in tmp:
                if j > s[i]:
                    s[i] = j
                    break
            # print(i)
            print(''.join(s[:i+1]))
            break
    else:
        print(-1)

Submission status: Accepted

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

if l <=25:
    for i in range(26):
        ch=chr(ord('a')+i)
        if ch not in S:
            S+=ch
            break
elif S=='zyxwvutsrqponmlkjihgfedcba':
    S=-1
else:
    index=24
    tmp=[ord(S[25])]
    while ord(S[index]) > ord(S[index+1]):
        tmp.append(ord(S[index]))
        index -= 1

    re=max(tmp)
    for ch in tmp:
        if ch < re and ch > ord(S[index]) :
            re = ch
    S=S[:index]+chr(re)

print(S)

Submission status: Accepted

Solution code:
S = input()

if len(S) < 26:
  N = [chr(i) for i in range(97,123) if chr(i) not in S]
  print(S + N[0])
else:
  for i in range(26):
    for j in range(i+1):
      if S[-(i + 1)] < S[-(j + 1)]:
          print(S[:-(i+1)]+S[-(j+1)])
          exit()
  else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
s = input()
counts = [0] * 26

for num in s:
    counts[ord(num) - 97] += 1
    
for i in range(26):
    if counts[i] == 0:
        print(s + chr(i + 97))
        sys.exit()

d = []
for i in reversed(range(len(s))):
    for num in d:
        if s[i] < num:
            print(s[0: i] + num)
            sys.exit()
        
    d.append(s[i])
    d.sort()

print(-1)

Submission status: Accepted

Solution code:
import string

S = input()
dictt = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13,
         'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
dic2 = ['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']

base = '0'
if len(S) < 26:
    for i in range(len(S)):
        bag = set()
        for s in S[:len(S)-i]:
            bag.add(dictt[s])
        for j in range(1, 27):
            if not j in bag:
                print(S+dic2[j-1])
                exit()
else:
    for i in range(len(S)):
        bag = set()
        for s in S[:len(S)-i]:
            bag.add(dictt[s])
        the_last_letter = (S[len(S)-i-1])
        avail_index = dictt[the_last_letter]
        for j in range(avail_index+1, 27):
            if not j in bag:
                print(S[:len(S)-i-1]+dic2[j-1])
                exit()
    print(-1)

Submission status: Accepted

Solution code:
import sys
from sys import stdin

s = stdin.readline().rstrip()
if len(s) == 26:
    count = 1
    for i in range(1, len(s)):
        if s[-i] < s[-i - 1]:
            count += 1
        else:
            break
    if count == 26:
        print(-1)
    else:
        for i in sorted(s[-count:]):
            if s[-count - 1] < i:
                print(s[:-count-1] + i)
                sys.exit()
else:
    d = {}
    for i in range(26):
        d.update({i: 0})
    for si in s:
        d[ord(si) - 97] += 1
    for i in range(26):
        if d[i] == 0:
            s += chr(i + 97)
            print(s)
            sys.exit()

Submission status: Accepted

Solution code:
S = input()
words = [chr(c) for c in range(ord("a"), ord("z")+1)]
if S == "".join(words[::-1]):
  print(-1)
elif len(S) != 26:
  for c in words:
    if c not in S:
      print(S + c)
      exit()
else:
  for i in range(1, 26):
    for j in range(i+1):
      if S[-(i+1)] < S[-(j+1)]:
        print(S[:-(i+1)] + S[-(j+1)])
        exit()

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    S=readline().strip()
    N=len(S)
    alps=[chr(i) for i in range(97,97+26)]
    flags=[False]*26
    if N<26:
        for i in range(N):
            flags[ord(S[i])-97]=True
        for i in range(26):
            if flags[i]==False:
                s=alps[i]
                break
        print(S+s)

    else:
        if S=="zyxwvutsrqponmlkjihgfedcba":
            print(-1)
        else:
            k=0
            for i in range(26):
                if i==0:
                    j=ord(S[i])-97
                else:
                    if ord(S[i-1])-97<ord(S[i])-97:
                        k=i
                        j=ord(S[i])-97

            target1=S[k-1]
            for i in range(k,26):
                if i==k:
                    target2=S[k]
                else:
                    if ord(target1)-97<ord(S[i])-97<ord(target2)-97:
                        target2=S[i]

            print(S[:k-1]+target2)

if __name__=="__main__":
    main()

Submission status: Accepted

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

def main():
    S = input()
    T = 'abcdefghijklmnopqrstuvwxyz'
    if len(S) < 26:
        U = set(T) - set(S)
        print(S + sorted(U)[0])
    else:
        if S == T[::-1]:
            print(-1)
        else:
            count = 1
            for i in range(25):
                if S[25-i] > S[24-i]:
                    break
                else:
                    count += 1
            #print(S[26-count:])
            for s in sorted(S[26-count:]):
                if s > S[25-count]:
                    print(S[:25-count] + s)
                    exit()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
alp = ['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']
buf = ''
for i in alp:
    if not i in set(s):
        buf = i
        break
if buf:
    print(s+buf)
else:
    c = 1
    for i in range(1,len(s)):
        if s[-i-1]>s[-i]:
            c += 1
        else:break
    if c==26:
        print(-1)
    else:
        for i in sorted(s[-c:]):
            if ord(s[-c-1])<ord(i):
                buf = i
                break
        print(s[:-c-1]+buf)

Submission status: Accepted

Solution code:
import sys
from string import ascii_lowercase as AL
input = sys.stdin.readline


def main():
    S = input().rstrip()

    if len(S) < 26:
        for a in AL:
            if a not in S:
                ans = S + a
                break
    else:
        i = 25
        while True:
            if i == 0:
                ans = -1
                break
            if S[i-1] > S[i]:
                i -= 1
            else:
                idx = AL.index(S[i-1])
                while True:
                    idx += 1
                    if AL[idx] in S[i:]:
                        next_al = AL[idx]
                        break
                ans = S[:i-1] + next_al
                break
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = input()

if len(S) < 26:
    st = set(S)
    for c in range(26):
        if not chr(ord('a') + c) in st:
            S += chr(ord('a') + c)
            break
    print(S)
else:
    i = len(S) - 1
    while i - 1 >= 0 and ord(S[i]) < ord(S[i - 1]):
        i -= 1
    if i == 0:
        print(-1)
    else:
        next_chr = ord(S[i - 1]) - ord('a')
        st = set(S[i:])
        for c in range(next_chr + 1, 26):
            if chr(ord('a') + c) in st:
                print(S[:i - 1] + chr(ord('a') + c))
                break

Submission status: Accepted

Solution code:
s = input()
c = len(s)-1
r = 0
x = 97
while -2 < c:
    for i in range(x, 97+26):
        if chr(i) not in s:          
            s = s+chr(i)
            r = 1
            break
    if r == 1:
        break
    elif c < 0:
        break
    else:
        x = ord(s[-1]) +1
        c -= 1
        s = s[:len(s)-1]
if r==0:
    s = '-1'
print(s)

Submission status: Accepted

Solution code:
s=str(input())
a=list("zyxwvutsrqponmlkjihgfedcba")
a=a[::-1]
#print(a)


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


if len(s)==26:
    s=s[::-1]#後ろから見ていく
    for i in range(len(s)):
        for j in range(i+1):
            #print(i,j,s[i],s[j])
            if s[i]<s[j]:#xyzの降順の流れが切れたところを変える
                s=s[::-1]
               # print(s)
                print(s[:-(i+1)]+s[-(j+1)])
                exit()
else:
    for i in a:
        if i not in s:
            s+=i
            break
    print(s)