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:
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
x=len(s)

al=[0]*26

if x!=26:
    for i in s:
        al[ord(i)-97]=1
    
    for i in range(26):
        if al[i]==0:
            s+=chr(97+i)
            print(s)
            exit()

else:
    option=[]
    
    for i in reversed(range(26)):
        option=sorted(option)
        num=ord(s[i])
        if i==25:
            option.append(num)
        else:
            if num>max(option):
                option.append(num)
            else:
                s=s[:26-len(option)-1]
                for i in option:
                    if i>num:
                        s+=chr(i)
                        print(s)
                        exit()

Submission status: Accepted

Solution code:
s=input()
a=97
if s=="zyxwvutsrqponmlkjihgfedcba":
    print("-1")
elif len(s)!=26:
    for i in range(26):
        if chr(a+i) not in s:
            print(s+chr(a+i))
            exit()
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()

Submission status: Accepted

Solution code:
s=input()
n=len(s)
cha=["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"]
if n<26:
    for i in range(26):
        if s.count(cha[i])==0:
            print(s+cha[i])
            exit()
else:
    for j in range(n-1,0,-1):
        if s[j]>s[j-1]:
            memo=j-1
            break
        if j==1:
            print(-1)
            exit()
    s2=s[:j]
    for i in range(26):
        if s2.count(cha[i])==0 and cha[i]>s[memo]:
            memo2=cha[i]
            break
    print(s[:j-1]+memo2)

Submission status: Accepted

Solution code:
S = input()

alf = set([chr(i) for i in range(ord('a'), ord('z')+1)])

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(S) < 26:
    not_used_alf = list(alf - set(S))
    ans = S + min(not_used_alf)
    print(ans)
else:
    tmp = S[-1]
    for i in reversed(range(26)):
        if S[i] >= tmp:
            tmp = S[i]
            continue
        else:
            break
    tmp = set(S[i+1:])
    for x in [chr(i) for i in range(ord('a'), ord('z')+1)]:
        if x in tmp and x > S[i]:
            ans = S[:i] + x
            break
    print(ans)

Submission status: Accepted

Solution code:
S = input()

if len(S) < 26:
    for c in [chr(i) for i in range(ord('a'), ord('a') + 26)]:
        # print(c)
        if c not in S:
            ans = S + c
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    for i in range(1, len(S)):
        j = - i
        if ord(S[j - 1]) < ord(S[j]):
            break
    # print(S[j:])
    change = 'z'
    for c in S[j:]:
        if ord(S[j - 1]) < ord(c) < ord(change):
            change = c
    ans = S[:j - 1] + change
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
 
S=list(input())
 
 
S=list(map(lambda x : ord(x)-97,S))
 
alpha=[0]*26
for s in S:
    alpha[s]=1
 
if len(S)!=26:
    for i in range(26):
        if alpha[i] == 0:
            S.append(i)
            ans=S[:]
            ans=list(map(lambda x :chr(x+97), ans))
            print("".join(ans))
            exit()
else:
    for i in range(26)[::-1]:
        if alpha[S[i]:].count(0)>0:
            for j in range(S[i],26):
                if alpha[j]==0:
                    ans=S[:i]+[j]
                    ans=list(map(lambda x :chr(x+97), ans))
                    print("".join(ans))
                    exit()
        else:
            alpha[S[i]]=0
 
print(-1)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

S=list(input())


S=list(map(lambda x : ord(x)-97,S))

alpha=[0]*26
for s in S:
    alpha[s]=1

if len(S)!=26:
    for i in range(26):
        if alpha[i] == 0:
            S.append(i)
            ans=S[:]
            ans=list(map(lambda x :chr(x+97), ans))
            print("".join(ans))
            exit()
else:
    for i in range(26)[::-1]:
        if alpha[S[i]:].count(0)>0:
            for j in range(S[i],26):
                if alpha[j]==0:
                    ans=S[:i]+[j]
                    ans=list(map(lambda x :chr(x+97), ans))
                    print("".join(ans))
                    exit()
        else:
            alpha[S[i]]=0

print(-1)

Submission status: Accepted

Solution code:
s = input()
cnt = [0]*26
ans = ""
if s[0] == "z":
    print(-1)
    exit(0)
for i in range(len(s)):
    if cnt[int(ord(s[i])-ord("a"))] == 1:
        min = chr(ord("z")+1)
        for j in range(26):
            if cnt[j] == 0:
                if int(ord(s[i])) < ord(chr(j+ord("a"))) < ord(min):
                    min = chr(j+ord("a"))
        ans += min
        print(ans)
        exit(0)
    else :
        ans += s[i]
        cnt[int(ord(s[i])-ord("a"))] = 1
if ans == s :
    min = chr(ord("z") + 1)
    for j in range(26):
        if cnt[j] == 0:
            if ord(chr(j+ord("a"))) < ord(min):
                min = chr(j+ord("a"))
    if min == chr(ord("z") + 1):
        if s[0] == "z":
            print(-1)
            exit(0)
        else:
            for k in range(len(s)-1,-1,-1):
                ans = ans.strip(s[k])
                cnt[int(ord(s[k])-ord("a"))] -= 1
                if ord(s[k]) > ord(s[k-1]):
                    ans = ans.strip(s[k-1])
                    min = chr(ord("z") + 1)
                    for j in range(26):
                        if cnt[j] == 0:
                            if int(ord(s[k-1])) < ord(chr(j + ord("a"))) < ord(min):
                                min = chr(j + ord("a"))
                    ans += min
                    break
    else:
        ans += min
print(ans)

Submission status: Accepted

Solution code:
d="zyxwvutsrqponmlkjihgfedcba"
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":print(-1)
else:
    if len(s)!=26:
        for i in range(1,1+26):
            if d[-i] not in s:print(s+d[-i]);exit()
    else:
        from bisect import bisect_left as bl,bisect_right as br
        de=[s[-1]]
        for i in range(2,27):

            if de[-1]<s[-i]:de.append(s[-i])
            else:print(s[:-i]+de[bl(de,s[-i])]);exit()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
s = list(s)
asc = range(97,123)

import bisect

if len(s) < 26:
    ss = [ord(u) for u in s]
    for i in asc:
        if i not in ss:
            s.append(chr(i))
            print(''.join(s))
            exit()
else:
    ss = s[::-1]
    a = [ord(ss[0])]
    for i in range(1,len(ss)):
        j = ord(ss[i])
        if j > a[-1]:
            a.append(j)
        else:
            k = bisect.bisect_right(a,j)
            x = chr(a[k])
            sss = s[:26-(i+1)] + [x]
            print(''.join(sss))
            exit()
    print(-1)

Submission status: Accepted

Solution code:
S = input()
if len(S) != 26:
    for i in range(26):
        if chr(ord('a') + i) not in S:
            print(S + chr(ord('a') + i))
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    can = []
    for i in range(26):
        if len(can) >= 1 and can[-1] > ord(S[-1-i]):
            for s in can:
                if s > ord(S[-1-i]):
                    print(S[:-1-i] + chr(s))
                    exit(0)
        can.append(ord(S[-1-i]))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    if len(s)==26:
        for i in range(25,0,-1):
            if s[i]<s[i-1]:
                continue
            else:
                sx=s[i-1]
                for j in range(1,25):
                    sy=chr(ord(sx)+j)
                    if sy in s[i:]:
                        print(s[:i-1]+sy)
                        sys.exit()
        else:
            print(-1)
    else:
        for i in range(26):
            if chr(ord('a')+i) in s:
                continue
            print(s+chr(ord('a')+i))
            break

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
alf=[chr(ord('a') + i) for i in range(26)]

s=input()

for i in s:
    if i in alf:
        alf.remove(i)

if len(alf) > 0:
    print(s+alf[0])
else:
    if s=='zyxwvutsrqponmlkjihgfedcba':
        print(-1)
        exit()
    else:
        ans=[]
        for i in range(1,27):
            for j in ans:
                if s[-i]<j:
                    print(s[:26-i]+j)
                    exit()
            ans.append(s[-i])
            ans.sort()

Submission status: Accepted

Solution code:
S = str(input())
alp = "abcdefghijklmnopqrstuvwxyz"
s = len(S)
for i in range(s):
  alp = alp.replace(S[i],'')
if alp != '':
  print(S+alp[0])
  exit()
else:
  if S == "zyxwvutsrqponmlkjihgfedcba":
    S = "-1"
  else:
    for j in range(s-1,0,-1):
      if S[j] > S[j-1]:
        alp = list(S[j-1:])
        alp = sorted(alp)
        for k in range(len(alp)+1):
          if alp[k] == S[j-1]:
            S = S[:j-1]+alp[k+1]
            print(S)
            exit()
print(S)

Submission status: Accepted

Solution code:
s=input()
# アルファベット(a〜z)→数値(1〜26)小文字限定
a2n = lambda c: ord(c) - ord('a') + 1
# 数値(1〜26)→アルファベット(a〜z)
n2a = lambda c: chr(c+64).lower()

if len(s)<26:
  for i in range(1,27):
    if n2a(i) not in s:
      print(s+n2a(i))
      exit()
f=False
for i in range(1,26):
  if a2n(s[-i-1])>a2n(s[-i]):
    continue
  else:
    f=True
    break
if f==False:
  print(-1)
  exit()

ans=s[:-i-1]
mini=min([a2n(d) for d in s[-i:] if a2n(d)>a2n(s[-i-1])])
ans+=str(n2a(mini))
print(ans)

Submission status: Accepted

Solution code:
s=input()
res=[0]*26
for i in range(len(s)):
    res[ord(s[i])-97]=1
ans="-1"
for i in range(26):
    if res[i]==0:
        ans=s+chr(i+97)
        break
if ans=="-1":
    for i in range(26):
        for j in range(ord(s[25-i])-96,26):
            if res[j]==0:
                ans=s[:25-i]+chr(j+97)
                break
        if ans!="-1":
            break
        res[ord(s[25-i])-97]=0
print(ans)

Submission status: Accepted

Solution code:
s = input()

a = list('abcdefghijklmnopqrstuvwxyz')

if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for x in a:
        if s.count(x) == 0:
            print(s + x)
            break
else:
    end = False
    for i in range(25)[::-1]:
        if end:
            break
        for j in range(i+1,26)[::-1]:
            if s[i] < s[j]:
                print(s[:i]+s[j])
                end = True
                break

Submission status: Accepted

Solution code:
S = input()

if len(S) != 26:
    chars = set()
    for s in S:
        chars.add(s)
    for c in range(ord('a'), ord('a') + 26):
        if chr(c) not in chars:
            print(S + chr(c))
            exit(0)

# len(S) == 26:
ans = S[0:25]
S = [ord(s) for s in S]
prev = [S[25]]
for i in range(24, -1, -1):
    res = 10000
    for p in prev:
        if S[i] < p:
            res = min(res, p)
    if res == 10000:
        prev.append(S[i])
    else:
        print(ans[0:i] + chr(res))
        exit(0)

print(-1)

Submission status: Accepted

Solution code:
#print([chr(ord('a') + i) for i in range(26)])
#print([chr(i) for i in range(97, 97+26)])
import sys
import bisect
li = [chr(i) for i in range(97, 97+26)]
s = input()
if len(s)<26:
    for l in li:
        if l not in s:
            print(s+l)
            sys.exit()
elif s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
else:
    r = s[::-1]
    tmp = ''
    kabe = ''
    for i,rr in enumerate(r):
        if i == 0:
            continue
        if rr < r[i-1]:
            tmp = r[:i+1][::-1]
            kabe = rr
            break
    #print(tmp) #wzyx

    tmp_li = list(tmp)
    tmp_li.sort()
    ind = bisect.bisect_left(tmp_li, kabe)
    s0 = tmp_li[ind+1]
    #s0 = tmp_li[1]
    print(s.replace(tmp, s0))

    #print(tmp_li)
    #print(s - tmp + tmp[-1])
    #xx = list(tmp[1:])
    #xx.sort()
    #print(s[:26-len(tmp)] + xx[0])

Submission status: Accepted

Solution code:
S=input()
s=[]
for i in range(26):
  s=s+[chr(97+i)]
for i in range(len(S)):
  s.remove(S[i])
if len(s)==0:
  for i in range(25):
    if ord(S[24-i])<ord(S[25-i]):
      q=S[25-i:]
      q2=[]
      for j in q:
        if ord(j)<ord(S[24-i]):
          pass
        else:
          q2+=[ord(j)]
      q2.sort()
      
      S=S[:24-i]+chr(q2[0])
      print(S)
      break
    if i==24:
      print(-1)
    
else:
  print(S+s[0])

Submission status: Accepted

Solution code:
alp=[chr(ord('a') + i) for i in range(26)]
unused=set(alp)
     
S=input()
#print("len",len(S))

if len(S)<26:
    for i in range(len(S)):
        if S[i] in unused:
            unused.remove(S[i])
    unused=sorted(unused)
    print(S+unused[0])
else:
    for j in range(len(S)-1,-1,-1):
        p=set(S[j:])
        #print(p)
        q=sorted(p)
        if S[j]==q[-1]:
            pass
        else:
            idx=q.index(S[j])
            print(S[:j]+q[idx+1])
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    S = input()
    A = set(chr(ord("a") + x) for x in range(26))
    if len(S) < 26:
        se = A - set(S)
        x = min(se)
        print(S + x)

    else:
        if S == "".join(sorted(A, reverse=True)):
            print(-1)
            return
        else:
            c = []
            S = list(S)
            while True:
                x = S[-1]
                if not c or (x > max(c)):
                    c.append(S.pop())
                    continue
                y = min(ch for ch in c if ch > x)
                S = "".join(S[:-1]) + y
                print(S)
                return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return "".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

s = list(input())[:-1]

ans = ""

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

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

if len(s) == 26:
    for i in range(25,-1,-1):
        if i == 25:
            po[ord(s[-1])-97] = 1
            continue
        else:
            if s[i] > s[i+1]:
                po[ord(s[i])-97] = 1
                po[ord(s[i+1])-97] = 1
            else:
                ans = jo(s[:i])
                for j in range(26):
                    if po[j] and ord(s[i])-97 < j:
                        ans += chr(97+j)
                        break
                

                break


else:
    for i in range(len(s)):
        on = ord(s[i])-97
        if po[on] == 0:
            ans += s[i]
            po[on] = 1
    for j in range(26):
        if po[j] == 0:
            ans += chr(97+j)
            break

print(ans)

Submission status: Accepted

Solution code:
S = input()
if len(S) == 26:
    last = None
    l = [False for i in range(26)]
    for i, s in enumerate(S):
        l[ord(s)-ord('a')] = True
        o = ord(s)-ord('a')
        while o <= ord('z')-ord('a'):
            if l[o] == False:
                last = S[:i] + chr(ord('a') + o)
                break
            o += 1
    if last:
        print(last)
    else:
        print(-1)
else:
    l = [False for i in range(26)]
    for s in S:
        l[ord(s)-ord('a')] = True
    for i in range(26):
        if l[i] == False:
            print(S+chr(ord('a') + i))
            break

Submission status: Accepted

Solution code:
import collections
S = input()

if(S == "zyxwvutsrqponmlkjihgfedcba"):
    print(-1)
    exit()
    
alp = "abcdefghijklmnopqrstuvwxyz"
r = S[::-1]

c = collections.Counter(S)
c_s = sorted(c.items(), key=lambda x:x[0])
tmp = ""

for i in range(len(c_s)):
    tmp += c_s[i][0]

if(len(S) < 26):
    for i in range(len(alp)):
        if(alp[i] not in tmp):
            print(S+alp[i])
            break
else:
    for i in range(len(S)-1):
        if(r[i] > r[i+1]):
            cha = sorted(r[:(i+1)])
            for j in range(len(cha)):
                if(cha[j] > r[i+1]):
                    print(S[:(len(S)-i-2)]+cha[j])
                    exit()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

S = input()

if S == "zyxwvutsrqponmlkjihgfedcba":
	print(-1)
elif len(S) < len(ascii_lowercase):
	for char in ascii_lowercase:
		if char not in S:
			print(S+char)
			break
else:
	S = S[::-1]
	for i in range(len(S)-1):
		if S[i+1] < S[i]:
			for char in sorted(S[:i+1]):
				if char > S[i+1]:
					print(S[::-1][:-i-2]+char)
					exit()

Submission status: Accepted

Solution code:
s = input()
c = list(s)
c2 = set(c)
al = sorted(list("qwertyuiopasdfghjklzxcvbnm"))
#26文字未満なら追加

if len(c)<26:
    for i in range(26):
        if al[i] not in c2:
            print(s+al[i])
            exit()

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

rev = "zyxwvutsrqponmlkjihgfedcba"
for i in range(25,-1,-1):
    x = sorted(c[i:])
    for j in x:
        if ord(s[i])<ord(j):
            print(s[:i]+j)
            exit()

Submission status: Accepted

Solution code:
def main():
    S = input()
    S_set = set(list(S))
    C_set = set([chr(ord("a") + i) for i in range(26)])

    if len(S_set) == len(C_set):
        S = list(S)
        p = None
        for i in range(len(S) - 2, -1, -1):
            if S[i] < S[i + 1]:
                p = i
                break
        else:
            print(-1)
            return

        ans = S[:p].copy()
        rest = C_set - set(S[: p + 1])
        rest = sorted(rest)
        for r in rest:
            if S[p] < r:
                ans.append(r)
                break
        print(*ans, sep="")
    else:
        diff = C_set - S_set
        diff = sorted(diff)
        print(S + diff[0])
        return


main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc022/tasks/agc022_a

from string import ascii_lowercase

s = input()
rems = [c for c in ascii_lowercase if c not in s]

if len(rems) > 0:
    print("{}{}".format(s, rems[0]))
    exit()
else:
    # 長さ26文字
    # S = [p1, ..., p26]

    # 右から見て照準である最長な文字列を探す
    j = len(s) - 1
    while s[j] < s[j - 1]:
        j -= 1

    if j == 0:
        print(-1)
    else:
        sj = s[:j - 1]
        pjm1 = s[j - 1]
        nc = min([c for c in s[j:] if c > pjm1])
        print(sj + nc)

Submission status: Accepted

Solution code:
s = list(input())
alph = [chr(i) for i in range(97, 123)]
alph_count = [0] * 26
for i in s:
    alph_count[alph.index(i)] = 1
ans = ["-1"]
if len(s) != 26:
    for i in range(26):
        if alph_count[i] == 0:
            ans = s + [alph[i]]
            break
else:
    for i in range(25, -1, -1):
        c = -1
        for j in range(26):
            if s[i] == alph[j]:
                c = j
            if not c == -1:
                if alph_count[j] == 0:
                    s[i] = alph[j]
                    ans = s[0 : i + 1]
            if not ans[0] == "-1":
                break
        alph_count[c] = 0
        if not ans[0] == "-1":
            break
print("".join(ans))

Submission status: Accepted

Solution code:
S = list(input())
N = len(S)
if N == 26:
    if S == list("zyxwvutsrqponmlkjihgfedcba"):
        print(-1)
    else:
        O = [0]*(N+1)
        for i in range(N):
            O[i] = ord(S[i])

        for i in range(N)[::-1]:
            co = [0]*N
            
            for j in range(1,25):

                for k in range(N):
                    co[k] = O[k]

                co[i] += j

                if co[i] > 122:
                    continue
                else:
                    if len(set(co[0:i+1])) == len(co[0:i+1]):
                        ans = co[0:i+1]
                        break

            else:
                continue
            break

        for i in range(len(ans)):
            ans[i] = chr(ans[i])

        cans = "".join(ans)
        print(cans)

else:
    #a97 z122
    O = [0]*(N+1)
    for i in range(N):
        O[i] = ord(S[i])

    for i in range(97,123):
        if i not in O:
            last = i
            break 
    
    O[N] = i

    for i in range(N+1):
        O[i] = chr(O[i])
    
    ans = "".join(O)
    print(ans)

Submission status: Accepted

Solution code:
S = list(input())
l = len(S)
if S == list('zyxwvutsrqponmlkjihgfedcba'):
    print(-1)
else:
    if l != 26:
        for i in range(26):
            if not chr(i+97) in S:
                S.append(chr(i+97))
                break
        print(''.join(S))
    else:
        li = []
        for i in range(26):
            c = S.pop()
            li.append(c)
            for x in li:
                if x>c:
                    S.append(x)
                    print(''.join(S))
                    break
            else:
                continue
            break

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc022/submissions/9146869

s = str(input())

# 英小文字26個
alphabet = set(chr(ord('a')+i) for i in range(26))

def F_short(s):
  # 未使用の文字で最小のものを後ろに付ける
  se = alphabet - set(s)
  x = min(se)
  return s + x

def F_long(s):
  # 辞書順いちばん最後 zyx...cba
  if s == ''.join(sorted(alphabet, reverse=True)):
    return -1

  # 1文字ずつpopして、増加チャンスを待つ
  cand = []  # 末尾から除去した文字たち
  s = list(s)
  while True:
    x = s[-1]  # 末尾の文字
    if (not cand) or (x > max(cand)):
      cand.append(s.pop())
      continue
    else:
      break
  y = min(ch for ch in cand if ch > x)
  return ''.join(s[:-1]) + y

if len(s) < 26:
  ans = F_short(s)
else:
  ans = F_long(s)
print(ans)

Submission status: Accepted

Solution code:
s = str(input())
ans = ''
if len(s) < 26:
  for i in range(ord('a'), ord('z')+1):
    if chr(i) not in s:
      ans = s + chr(i)
      break
else:
  for i in range(24, -1, -1):
    if ord(s[i]) < ord(s[i+1]):
      ans = s[:i]
      for j in range(ord(s[i])+1, ord('z')+1):
        if chr(j) not in ans:
          ans += chr(j)
          break
      break
if ans: print(ans)
else: print(-1)

Submission status: Accepted

Solution code:
#辞書順
#a<b,a*<b(先頭が大きいため)
#a
from itertools import permutations
S=input()
N=len(S)
alp=[0]*26
searched=[0]*26
flag=0
c=0
sub=100
minsub=100
#zxywvutsrqponmlkjihgfedcba→
for i in range(N):
    alp[ord(S[i])-97]=alp[ord(S[i])-97]+1
    #print(alp)
if (max(alp)!=1)or(S=="zyxwvutsrqponmlkjihgfedcba"):
    flag=0
else:
    if N!=26:
        for i in range(26):
            if alp[i]==0:
                c=chr(97+i)
                S="".join((S,c))
                flag=1
                break
    else:
        i=len(S)-2
        s=S[i:]
        #print(s)
        while True:
            piv=s[0]
            #print(s,S[:i],piv)
            for j in range(len(s)-1):
                if ord(s[j+1])>ord(piv):
                    sub=ord(s[j+1])-ord(piv)
                    #print(sub)
                    if sub<minsub:
                        minsub=sub
            if (minsub!=100):
                #print(minsub,chr(ord(piv)+minsub))
                S="".join((S[:i],chr(ord(piv)+minsub)))
                flag=1
                break

            minsub=100
            sub=0
            i=i-1
            s=S[i:]

if flag==0:
    print("-1")
else:
    print(S)

Submission status: Accepted

Solution code:
s = input()
list_s = list(s)
len_s = len(list_s)
check = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in range(26):
    if check[i] not in list_s:
        print(s + check[i])
        exit()
for i in range(1, len_s + 1):
    memo = list_s.pop(len_s - i)
    for j in range(check.index(memo) + 1, 26):
        if check[j] not in list_s:
            print(s[:26 - i] + check[j])
            exit()
print(-1)

Submission status: Accepted

Solution code:
s = input()
s_origin = s
n = len(s)

for i in range(n):
    for j in range(ord("a"), ord("z")+1):
        if not (chr(j) in s) and \
            s + chr(j) != s_origin and \
                s_origin < s + chr(j):
            print(s + chr(j))
            exit()
    if len(s) == 1 and s != "z":
        print(chr(ord(s) + 1))
        exit()
    s = s[:-1]

print(-1)

Submission status: Accepted

Solution code:
S = input()
alpha = list("abcdefghijklmnopqrstuvwxyz")

if len(S) < 26:
    s = set(list(S))
    for i in range(26):
        if alpha[i] not in s:
            S += alpha[i]
            break
else:
    flag = False
    for i in range(24, -1, -1):
        tmp = ord(S[i])
        for j in range(25, i, -1):
            if tmp < ord(S[j]):
                S = S[:i] + S[j]
                flag = True
                break    
        
        if flag:
            break
    else:
        S = "-1"

print(S)

Submission status: Accepted

Solution code:
s = input()
alf = 'abcdefghijklmnopqrstuvwxyz'
pos = {}
pos_alf = {c:i for i,c in enumerate(alf)}

for i,c in enumerate(s):
    if c not in pos:
        pos[c] = []
    pos[c] = i

# if s == 'zyxwvutsrqponmlkjihgfedcba':
#     print(-1)
#     exit()
# el
if len(s) != 26:
    for c in alf:
        if c not in pos:
            print(s+c)
            exit()

tmp = [pos_alf[s[-1]]]
for i in range(len(s)-1)[::-1]:
    if pos_alf[s[i]] < max(tmp):
        d = [j for j in tmp if j > pos_alf[s[i]]]
        print(s[:i]+alf[min(d)])
        break
    else:
        tmp.append(pos_alf[s[i]])
    # print(tmp)
else:
    print(-1)

Submission status: Accepted

Solution code:
s = input()

if len(s)<26:
        st = set(s)
        for c in "abcdefghijklmnopqrstuvwxyz":
                if not c in st:
                        print(s+c)
                        break
        else: print(-1)
else:
        prev = ord('a') - 1
        i = len(s)
        for c in s[::-1]:
                if ord(c)>prev: i -= 1
                else: break
                prev = ord(c)
        if i==0: print(-1)
        else:
                for c in sorted(s[i-1:]):
                        if ord(c)>ord(s[i-1]):
                                print(s[:i-1] + c)
                                break

Submission status: Accepted

Solution code:
def main():
    s = input()
    alphabet = "".join([chr(i) for i in range(97, 97 + 26)])
    if len(s) < 26:
        for i in alphabet:
            if i not in s:
                print(s + i)
                exit()
    else:
        if s == "zyxwvutsrqponmlkjihgfedcba":
            print(-1)
        else:
            for i in reversed(range(26)):
                for j in reversed(range(i + 1, 26)):
                    if s[i] < s[j]:
                        print(s[:i] + s[j])
                        exit()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import string
def f(x, mn=None):
    for c in string.ascii_lowercase:
        if c in x: continue
        if mn and c <= mn: continue
        return c
    return None

if n < 26:
    print(S + f(S))
    exit()

i = n-2
while i >= 0:
    if S[i] < S[i+1]: break
    i -= 1
else:
    print(-1)
    exit()

t = S[:i]
print(t + f(t, S[i]))

Submission status: Accepted

Solution code:
s=input()
alp="".join([chr(i) for i in range(97,97+26)])
if len(s)<26:
    for i in alp:
        if i not in s:
            print(s+i);exit()
else:
    if s=="".join([chr(i) for i in range(97,97+26)][::-1]):
        print(-1)
    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()

Submission status: Accepted

Solution code:
s=input()
alp="".join([chr(i) for i in range(97,97+26)])
if len(s)<26:
    for i in alp:
        if i not in s:
            print(s+i)
            exit()
else:
    if s=="".join([chr(i) for i in range(97,97+26)][::-1]):
        print(-1)
    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()