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 len(S) < 26:
    for i in range(26):
        c = chr(ord('a') + i)
        if c in S: continue
        print(S + c)
        exit()

cset = set()
for i,(a,b) in enumerate(reversed(list(zip(S,S[1:])))):
    cset.add(b)
    if a < b:
        last = 'z'
        for c in cset:
            if c < a: continue
            last = min(last, c)
        print(S[:-i-2] + last)
        exit()
print(-1)

Submission status: Accepted

Solution code:
S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
if len(S) < 26:
    for i in range(26):
        c = chr(ord('a') + i)
        if c in S: continue
        print(S + c)
        exit()

cset = set()
for i,(a,b) in enumerate(reversed(list(zip(S,S[1:])))):
    cset.add(b)
    if a < b:
        last = 'z'
        for c in cset:
            if c < a: continue
            last = min(last, c)
        print(S[:-i-2] + last)
        exit()

Submission status: Accepted

Solution code:
s = input()

g = set()
for i in s:
    g.add(i)

al = [chr(ord('a') + i) for i in range(26)]
a = 'abcdefghijklmnopqrstuvwxyz'

if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for w in al:
        if w in g:
            continue
        s += w
        print(s)
        break
else:
    for i in reversed(range(26)):
        j = a.find(s[i])
        g.remove(a[j])
        while j < 25:
            j += 1
            s = s[:-1] + a[j]
            if not a[j] in g:
                print(s)
                exit()
        s = s[:-1]

Submission status: Accepted

Solution code:
s = input()

alphaset = set()
for i in range(97, 97+27):
    alphaset.add(chr(i))
    

if len(s) < 26:
    for c in list(s):
        alphaset.remove(c)
        
    alphalist = list(alphaset)
    alphalist.sort()
    
    print(s + alphalist[0])
    
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        
    else:
        c = list(s)
        point = 100
        for i in range(25,0,-1):
            if c[i-1] > c[i]:
                continue
            else:
                old = c[i-1]
                for ci in c[:i]:
                    alphaset.remove(ci)
                    
                alphalist = list(alphaset)
                alphalist.sort()
                
                j = 0
                while old > alphalist[j]:
                    j += 1
                c[i-1] = alphalist[j]
                point = i
                break
        
        print("".join(c[:point]))

Submission status: Accepted

Solution code:
def f(s, i, d):
  if len(d) == 0:
    return "-1"
  if len(s) == i:
    d.sort()
    return d[0]
  d.remove(s[i])
  r = f(s, i+1, d)
  if r == "-1":
    d.sort()
    for j in d:
      if s[i] < j:
        return j
    d.append(s[i])
    return "-1"
  return "".join([s[i], r])

def main():
  d = list("abcdefghijklmnopqrstuvwxyz")
  t = input()
  r = f(t, 0, d)
  print(r)

main()

Submission status: Accepted

Solution code:
s = input()

ans = s
ansa = s
a = 'abcdefghijklmnopqrstuvwxyz'
a = [a[i] for i in range(26)]
if len(s)<26:
    for i in a:
        if ans.count(i) == 0:
            ans += i
            break
    print(ans)
else:
    b = 26
    for i in range(1,26):
        if a.index(s[i]) > a.index(s[i-1]):
            b = i
    if b == 26:
        print(-1)
    else:
        s = s[:b]
        for j in a:
            if a.index(j) > a.index(s[b-1]) and j not in s:
                s = s[:b-1]+j
                break
        print(s)

Submission status: Accepted

Solution code:
s = input()
from string import ascii_lowercase
a = ascii_lowercase
if s == a[::-1]:
  print(-1)
else:
  if len(set(s))<26:
    for ele in a:
      if ele not in s:
        print(s+ele)
        break
  else:
    suf=[s[-1]]
    for i in range(25, 0, -1):
      if s[i]<s[i-1]:
        suf.append(s[i-1])
      else:
        last = i-1
        break
    suf.sort()
    for i in range(len(suf)):
      if s[last]<suf[i]:
        print(s[:last]+suf[i])
        break

Submission status: Accepted

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

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

alphabet = ["a", "b", "c", "d", "e", "f", "g", "h","i", "j", "k","l","m","n","o", "p","q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
for i in alphabet:
    if not i in s:
        print(s + i)
        exit()

flag = True
while n > 0 and flag:
    last = alphabet.index(s[-1])
    for i in alphabet[last + 1:]:
        if i not in s[:-1]:
            print(s[:-1] + i)
            flag = False
            break
    else:
        s = s[:-1]
    n = len(s)

Submission status: Accepted

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

if S == "zyxwvutsrqponmlkjihgfedcba":
     print(-1)
        
elif len(S) < 26:
    print(S + min(set(alphabet) - set(S)))
    
else:
    for i in range(1, 27):
        if list(S)[-i] > list(S)[-(i + 1)]:
            break
    T = sorted(list(S)[-i:])
    #print(T)
    for t in T:
        if t > list(S)[-(i + 1)]:
            break
    print("".join(list(S)[:-(i + 1)]) + t)

Submission status: Accepted

Solution code:
S=input()

if(len(S)!=26):
 for i in range(ord("a"),ord("z")+1):
  CHARA=chr(i)
  if CHARA not in S:
   print(S+CHARA)
   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 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:
def main():
    s = input()

    ch_lst = [0 for i in range(26)]
    for i in s:
        ch_lst[ord(i)-ord('a')] += 1

    for i in range(len(ch_lst)):
        if ch_lst[i] == 0:
            s += chr(i+ord('a'))
            break

    else:
        for i in range(len(s)-1, -1, -1):
            ch_lst[ord(s[i])-ord('a')] = 0

            for j in range(ord(s[i])-ord('a')+1, 26):
                if ch_lst[j] == 0:
                    s = s[:i]
                    s += chr(j+ord('a'))
                    break

            else:
                continue

            break

        else:
            s = -1

    print(s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = raw_input()
if len(s) < 26:
    l = [ chr(ord('a') + i) for i in range(26) ]
    for c in s:
        l.remove(c)
    print s + l[0]
else:
    l = []
    p = -1
    for i in range(25, 0,-1):
        l.append(s[i])
        if s[i-1] < s[i]:
            l.append(s[i-1])
            p = i-1
            break
    if p == -1:
        print -1
    else:
        l.sort()
        print s[:p] + l[l.index(s[p]) + 1]

Submission status: Accepted

Solution code:
S = input()
T = "abcdefghijklmnopqrstuvwxyz"
A = [str(x) for x in T]

if len(S) != 26:
    for l in S:
        A.remove(l)
    print(S+A[0])
    exit()

if S == T[::-1]:
    print(-1)
    exit(0)

U = []
for k in range(1,26):
    if S[-k-1] > S[-k]:
        U.append(S[-k-1])
        U.append(S[-k])
    else:
        U.append(S[-k-1])
        U.append(S[-k])
        t = S[-k-1]
        S = S[:-k-1]
        break

U.sort()
for l in U:
    if t < l:
        print(S+l)
        exit(0)

Submission status: Accepted

Solution code:
import sys

def intstr(lis):
    a = ''
    for v in lis:
        a += chr(v+97)
    return a

s = list(input())
numS = []
for v in s:
    numS.append(ord(v)-97)#0-25

if len(numS)<26:
    for i in range(0,26):
        if numS.count(i)==0:
            numS.append(i)
            ans = intstr(numS)
            print(ans)
            sys.exit()
else:
    while numS:
        cur = numS.pop()
        for i in range(cur+1,26):
            if numS.count(i)==0:
                numS.append(i)
                ans = intstr(numS)
                print(ans)
                sys.exit()
    print(-1)

Submission status: Accepted

Solution code:
s = input()
old = ''
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    for i, s1 in enumerate(s[::-1]):
        if i != 0 and s1 < old[-1]:
            out = s[:len(s)-i-1] + min([o for o in old if o > s1])
            print(out)
            exit()
        old += s1
    print(-1)
else:
    print(s + min(set(sample)-set(s)))

Submission status: Accepted

Solution code:
s = input()
old = ''
flag = 0
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    for i, s1 in enumerate(s[::-1]):
        if i != 0 and s1 < old[-1]:
            out = s[:len(s) - i - 1] + min([o for o in old if o > s1])
            print(out)
            exit()
        old += s1
    if flag == 0:
        out = -1
else:
    out = s + min(set(sample)-set(s))

print(out)

Submission status: Accepted

Solution code:
s = input()
old = ''
flag = 0
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    for i, s1 in enumerate(s[::-1]):
        if i != 0 and s1 < old[-1]:
            for o in sorted(old):
                if o > s1:
                    out = s[:len(s)-i-1] + o
                    print(out)
                    exit()
        old += s1
    if flag == 0:
        out = -1
else:
    out = s + min(set(sample)-set(s))

print(out)

Submission status: Accepted

Solution code:
s = input()
old = ''
flag = 0
sample = [chr(i) for i in range(97, 97+26)]
if len(s) == 26:
    u = {s[-1]}
    for i in range(len(s) - 1)[::-1]:
        x = [c for c in u if s[i] < c]
        if x:
            out = s[:i] + min(x)
            break
        u.add(s[i])
    else:
        out = -1
    # for i, s1 in enumerate(s[::-1]):
    #     if i != 0 and s1 < min(old):
    #         out = s[:len(s)-i-1] + min(old)
    #         flag = 1
    #         break
    #     old += s1
    #     if flag == 0:
    #         out = -1
else:
    out = s + min(set(sample)-set(s))

print(out)

Submission status: Accepted

Solution code:
s = input()
t = set(map(chr, range(ord('a'), ord('z')+1))) - set(s)
if t:
    print(s + min(t))
else:
    u = {s[-1]}
    for i in range(len(s)-1)[::-1]:
        x = [c for c in u if s[i]<c]
        if x:
            print(s[:i] + min(x))
            break
        u.add(s[i])
    else:
        print(-1)

Submission status: Accepted

Solution code:
S = input()
chars = "abcdefghijklmnopqrstuvwxyz"
if len(S) < 26:
    for c in chars:
        if c not in S:
            Ans = S+c
            break
else:
    if S == chars[::-1]:
        Ans = -1
    else:
        for i in range(25):
            if S[25-i] > S[25-i-1]:
                break
        for j in range(25): # index out of rangeにならない？大丈夫？
            if chr(ord(S[25-i-1])+1+j) not in S[0:25-i-1]:
                break
        Ans = S[0:25-i-1]+chr(ord(S[25-i-1])+1+j)
print(Ans)

Submission status: Accepted

Solution code:
s = input()

#末尾
a = ord("a")
z = ord("z")

for i in range(a, z + 1):
    if not chr(i) in s:
        ans = s + chr(i)
        print(ans)
        exit()

# 26word
deleted = []
current = s
for i in reversed(s):
    for usable in deleted:
        if ord(current[-1]) <  usable:
            print(current[:-1] + chr(usable))
            exit()

    deleted.append(ord(i))
    sorted(deleted)
    current = current[:-1]

print("-1")

Submission status: Accepted

Solution code:
s= input()
V = list(s)
X = [chr(ord('a') + i) for i in range(26)]

if len(s)<26:
  for i in range(26):
    count1 = s.count(X[i])
    if count1 >= 1:
      continue
    else:
      Nasi = X[i]
      break
  ans = s + Nasi
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    ans= -1
  else:
    for i in range(25, 0, -1):
        if V[i] > V[i-1]:
          ans = V[0:i-1]
          if ans == []:
            ans = chr(ord(V[0]) + 1)
          else:
              B = min(V[i:26])
              while B <= V[i-1]:
                  B = chr(ord(B) + 1)
              while B in ans:
                  B = chr(ord(B) + 1)
              ans.append(B)
              ans = ''.join(ans)
              break
        else:
            continue
print(ans)

Submission status: Accepted

Solution code:
s = s0 = input()
d = {chr(c) for c in range(97, 123)}
for c in s:
    d.remove(c)
if d:
    s += min(d)
else:
    for c in reversed(s):
        s = s[:-1]
        if d and any(c < e for e in d):
            s += min(e for e in d if c < e)
            break
        d.add(c)
print(s if s else -1)
if s:
    assert s0 < s

Submission status: Accepted

Solution code:
S = input()
A = [chr(ord("a")+i) for i in range(26)]

if len(set(S)) < 26:
    for a in A:
        if a not in S:
            print(S+a)
            break
else:
    X = []
    for i in range(25, 0, -1):
        if S[i] > S[i-1]:
            X.append(S[i])
            X = sorted(list(filter(lambda x: x > S[i-1], X)))
            print(S[:i-1]+X[0])
            break
        else:
            X.append(S[i])
    else:
        print(-1)

Submission status: Accepted

Solution code:
def solve(S):
    chars = [chr(c) for c in range(ord("a"), ord("z") + 1)]
    cs = dict((c, S.count(c)) for c in chars)
    l = len(S) 
    if l < len(chars):
        return S + sorted(c for c, n in cs.items() if n == 0)[0]
    for i in range(2, l + 1):
        sl = S[:l - i]
        sc = S[l - i]
        sr = S[l - i + 1:]
        msrs = [c for c in sr if c > sc]
        if msrs:
            return sl + sorted(msrs)[0]
    return -1

S = input()
print(solve(S))

Submission status: Accepted

Solution code:
S = input()
if len(S) < 26:
    for i in range(26):
        c = chr(i + ord('a'))
        if c not in S:
            print(S + c)
            break
else:
    tmp = 26
    for i in range(len(S) - 1, 0, -1):
        if ord(S[i - 1]) < ord(S[i]):
            break
        tmp = i
    tmp -= 1
    if tmp == 0:
        print(-1)
    else:
        c = ord(S[:tmp][-1])
        suf = S[tmp:]
        print(S[:tmp - 1] + chr(min([ord(si) for si in suf if ord(si) > c])))

Submission status: Accepted

Solution code:
import string

S = input()
reversedS = ''.join(list(S)[::-1])
alphabets = list(string.ascii_lowercase)
lastWord = ''.join(alphabets[::-1])
cnt = [S.count(chr(ord('a')+i)) for i in range(26)]
if len(S) == 26:
    if S == lastWord: print(-1)
    else:
        found = False
        for i in range(len(reversedS)):
            s = reversedS[i]
            largeS = alphabets[ord(s)-ord('a')+1:]
            tailS = list(S)[26-i-1:]
            for lS in largeS:
                if lS in tailS:
                    print(S[:26-i-1]+lS)
                    found = True
                    break
            if found: break
else:
    for j in range(26):
        if cnt[j] == 0:
            S += chr(ord('a')+j)
            print(S)
            break

Submission status: Accepted

Solution code:
ars = "abcdefghijklmnopqrstuvwxyz"
S = input()
def iftasai(s):
  for i in s:
    if s.count(i) > 1:
      return False
  return True
if len(S) != 26:
  for i in ars:
    if not (i in S):
      print(S + i)
      quit()
elif S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  quit()
else:
  for i in range(1,len(S)+1):
    n = ars.index(S[-i])
    tmp = ars[n+1:]
    for j in tmp:
      ans = S[:-i] + j
      if iftasai(ans):
        print(ans)
        quit()

Submission status: Accepted

Solution code:
ars = "abcdefghijklmnopqrstuvwxyz"
S = input()
def iftasai(s):
  for i in s:
    if s.count(i) > 1:
      return False
  return True
if len(S) != 26:
  for i in ars:
    if not (i in S):
      print(S + i)
      quit()
elif S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  quit()
else:
  for i in range(1,len(S)+1):
    n = ars.index(S[-i])
    tmp = ars[n+1:]
    for j in tmp:
      ans = S[:-i] + j
      if iftasai(ans):
        print(ans)
        quit()
  n = ars.index(S[0])
  print(ars[n+1:])

Submission status: Accepted

Solution code:
#agc022a

c='abcdefghijklmnopqrstuvwxyz'
s=raw_input()
if len(s)<26:
 for v in c:
  if v not in s:
   res=s+v
   print res
   break
elif s==c[::-1]:
 print -1
else:
 s=s[::-1]
 i=0
 t=''
 while s[i]<s[i+1]:
  t+=s[i]
  i+=1
 t+=s[i]
 l=s[i+1:]
 r=ord('z')
 for v in t:
  if ord(v)>ord(l[0]):
   r=min(ord(v),r)
 res=chr(r)
 res=l[::-1][:-1]+res
 print res

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
eng_li = []
removed_list = []
all_letter = 26
for i in range (97,97+all_letter):
    eng_li.append(chr(i))
 
string = list(raw_input().strip())
length = len(string)
if length < all_letter:
    for i in string:
        eng_li.remove(i)
    string.append(eng_li[0])
    answer = "".join(string)
else:
    for i in range(all_letter-1):
        if ord(string[-1]) < ord(string[-2]) :
            removed_list.append(string[-1])
            string.pop()
        else:
            removed_list.append(string[-1])
            string.pop()
            removed_list=filter(lambda x:x>string[-1],sorted(removed_list))
            string.pop()
            string.append(removed_list[0])
            answer ="".join(string)
            break
    else:
        answer = -1
print answer

Submission status: Accepted

Solution code:
a=input().strip()
if len(a)<26:
  for i in range(97, 97+26):
    if chr(i) not in list(a):
      print(a+chr(i))
      break
else:
  f=False
  for i in range(26):
    t0=a[25-i:]
    ts=list(t0)
    ts.sort(reverse=True)
    t1="".join(ts)
    
    if t0 != t1 :
      for j in range(len(ts)):
        if ts[len(ts)-1-j]==t0[0]:
          print(a[:25-i]+ts[len(ts)-1-j-1])
          f=True
          break
    if f == True:
      break
  else:
    print(-1)

Submission status: Accepted

Solution code:
S = input()
cnt = [S.count(chr(ord('a')+i)) for i in range(26)]
if sum(cnt) == 26:
    pos = 0
    for j in range(25, 0, -1):
        if S[j] > S[j-1]:
            pos = j
            break
    if pos == 0:
        print(-1)
    else:
        a = S[pos:26]
        for x in a[::-1]:
            if S[pos-1] < x:
                T = S[:pos-1] + x
                print(T)
                break


else:
    for j in range(26):
        if cnt[j] == 0:
            S += chr(ord('a')+j)
            print(S)
            break

Submission status: Accepted

Solution code:
import string
 
S = input()
if len(S) < 26:
    for c in string.ascii_lowercase:
        if c not in S:
            break
    print(S + c)
else:
    ret = None
    for i in range(1, 26):
        idx = 25 - i
        if max(S[idx:]) != S[idx]:
            mc = min([c for c in S[(idx+1):] if c > S[idx]])
            ret = S[:idx] + mc
            break
    if ret is None:
        print(-1)
    else:
        print(ret)

Submission status: Accepted

Solution code:
import string
 
S = input()
if len(S) < 26:
    for c in string.ascii_lowercase:
        if c not in S:
            break
    print(S + c)
else:
    ret = None
    for i in range(26):
        if max(S[25-i:]) != S[25-i]:
            mc = 'z'
            for c in S[25-i:]:
                if c > S[25-i]:
                    mc = min(c, mc)
            ret = S[:25-i] + mc
            break
            
    if ret is None:
        print(-1)
    else:
        print(ret)

Submission status: Accepted

Solution code:
S = input()

if len(S) < 26:
    for c in 'abcdefghijklmnopqrstuvwxyz':
        if c not in S:
            print(S + c)
            break
else:
    cs = {S[-1]}
    for i in range(25)[::-1]:
        if S[i] < S[i + 1]:
            for c in sorted(list(cs)):
                if c > S[i]:
                    print(S[:i] + c)
                    break
            break
        else:
            cs.add(S[i])
    else:
        print('-1')

Submission status: Accepted

Solution code:
s = input()
if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
ns = [ord(c) - 97 for c in s]
ns_ = [i for i in range(26) if i not in ns]
if len(ns) == 26:
    ms = [0]
    while True:
        ms.append(ns.pop())
        if ms[-1] < ms[-2]:
            min_m = 27
            for m in ms:
                if m < min_m and m > ms[-1]:
                    min_m = m
            ns.append(min_m)
            break
else:
    ns.append(min(ns_))
cs = [chr(n + 97) for n in ns]
print(''.join(cs))

Submission status: Accepted

Solution code:
def solve(s):
  cnt="abcdefghijklmnopqrstuvwxyz"
  res=[0 for x in range(26)]
  for i in range(len(s)):
    a=cnt.index(s[i])
    res[a]+=1
  for i in range(26):
    if res[i]==0:
      return s+cnt[i]

def solve26(ss):
  s=ss
  cnt="abcdefghijklmnopqrstuvwxyz"
  pos=s[-1]
  res=cnt.index(pos)+1
  s=s[:-1]
  for i in range(res,26):
    jud=True
    pos=cnt[i]
    for j in range(len(s)):
      if s[j]==cnt[i]:
        jud=False
        break
    if jud:
      return s+pos
  return solve26(s)

S=input()
if len(S)==26 and S=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
elif len(S)==26:
  S=solve26(S)
  print(S)
else:
  S=solve(S)
  print(S)

Submission status: Accepted

Solution code:
from string import ascii_lowercase

S = raw_input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print -1
elif len(S) == 26:
    S = [ascii_lowercase.index(s) for s in S]
    while True:
        S[-1] += 1
        if S[-1] >= 26:
            S.pop()
        elif len(set(S)) == len(S):
            break
    print ''.join([ascii_lowercase[i] for i in S])
else:
    for a in ascii_lowercase:
        if a not in S:
            print S + a
            break

Submission status: Accepted

Solution code:
import sys

alphabets = "abcdefghijklmnopqrstuvwxyz"
S = input()
mp = {}

for s in S:
    if s in mp.keys():
        mp[s] = mp[s] + 1
    else:
        mp[s] = 1

def idx(c):
    if len(c) != 1:
        print("error")
        return None # invalid
    for i, e in enumerate(alphabets):
        if c == e:
            return i
    print("error")
    return None # invalid

# 使われてない文字がある場合
unused = []
for a in alphabets:
    if a not in mp.keys():
        unused.append(a)
        
if len(unused) > 0:
    print(S + unused[0])
    sys.exit(0)

# 先頭から探していく
pos = len(S) - 1
while True:
    if pos == 0:
        print(-1)
        sys.exit()
    x = S[pos-1]
    y = S[pos]
    if idx(x) < idx(y):
        # 書き換え
        minc = 'z'
        unused.append(y)
        for c in unused:
            if idx(minc) > idx(c) and idx(c) > idx(x):
                minc = c
        print(S[:pos-1] + minc)
        sys.exit()
    else:
        unused.append(y)

    # update
    pos = pos - 1

"""
abcdefghijklmnopqrstuvwzyx
abcdefghijklmnopqrstuvzyxw
azyxwvutsrqponmlkjihgfedcb
"""

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s = input()
    alpha = list([1 for i in range(26)])
    if len(s) != 26:
        for i in range(len(s)):
            if(alpha[ord(s[i])-97] == 0):
                print("-1")
                exit()
            alpha[ord(s[i])-97] = 0
        for i in range(len(alpha)):
            if alpha[i]==1:
                print(str(s)+chr(i+97))
                exit()
        print("-1")
        exit()
    else:
        max = ord(s[-1])-97
        alpha[ord(s[-1])-97] = 0
        
        for i in reversed(range(len(s)-1)):
            if ord(s[i])-97 <  max:
                for j in range(len(alpha)):
                    if alpha[j] == 0 and ord(s[i])-97<j:
                        print(s[:i]+chr(j+97))
                        exit()
            else:
                max = ord(s[i])-97
            alpha[ord(s[i])-97] = 0
        print("-1")
        exit()

Submission status: Accepted

Solution code:
import string


def main():
    """
    abcdefghijklmnopqrstuvwxyz
    zyxwvutsrqponmlkjihgfedcba
    """
    S = input()

    az = string.ascii_lowercase
    za = az[::-1]
    if len(S) < len(az):
        # 全文字使ってない
        s_s = set(S)
        s_az = set(az)
        a = sorted(list(s_az - s_s))[0]

        # S=abのとき、abc < ac==True なので追加で良い
        ans = S + a
        print(ans)
        return

    editorial(S)

def editorial(S):
    for i in range(1, 26)[::-1]:
        if S[i-1] > S[i]:
            continue

        # 逆sort済みの途中で辞書順になった
        # 逆sortの中で辞書順小さいもので置き換えて、辞書順最小にする
        suffix = S[i:]
        p_1 = S[i-1]
        for q in sorted(suffix):
            if q > p_1:
                ans = S[:i-1] + q
                print(ans)
                return
    print(-1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()

def itoc(i): return chr(ord('a') + i)
def ctoi(c): return ord(c) - ord('a')

if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
if len(S) == 26:
    i = 25
    while i > 0:
        if S[i-1] > S[i]:
            i -= 1
        else:
            ans = S[:i-1]
            for ci in range(ctoi(S[i-1])+1, 27):
                if itoc(ci) in ans: continue
                ans += itoc(ci)
                print(ans)
                exit()
else:
    for i in range(26):
        c = itoc(i)
        if c in S: continue
        print(S + c)
        break

Submission status: Accepted

Solution code:
S = input()

def solve():
    alpha = [c for c in 'abcdefghijklmnopqrstuvwxyz']
    if len(S) < 26:
        for S_i in S:
            alpha.remove(S_i)
        print(S + alpha[0])
        return
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
        return
    last_alpha = ""
    post_fix = ""
    latest = -1
    post_fix_index = 25
    for S_i in S[::-1]:
        if last_alpha == "" or S_i > last_alpha:
            last_alpha = S_i
            post_fix = S_i + post_fix
            post_fix_index -= 1
            if latest == -1:
                latest = S_i
        else:
            alpha_post_fix = sorted([c for c in post_fix])
            for c in alpha_post_fix:
                if S[post_fix_index] < c:
                    print(S[:post_fix_index] + c)
                    return


solve()

Submission status: Accepted

Solution code:
s = input()
x = "abcdefghijklmnopqrstuvwxyz"
if len(s)!=26:
	for i in x:
		 if i not in s:
		 	letter = i
		 	break
	print(s+letter)
elif s=="zyxwvutsrqponmlkjihgfedcba":
	print(-1)
else:
	for i in range(24,-1,-1):
		if x.find(s[i])<x.find(s[i+1]):
			s,s2,y = s[:i],s[i:],s[i]
			break
	s3 = []
	for i in s2: s3.append(i)
	s3.sort()
	letter = s3[s3.index(y)+1]
	print(s+letter)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# AtCoder Grand Contest
# Problem A


# See:
# https://img.atcoder.jp/agc022/editorial.pdf
# https://agc022.contest.atcoder.jp/submissions/2286541
def determine_next_word(given_word: str) -> str:
    while given_word:
        pi = ascii_lowercase.index(given_word[-1])
        given_word = given_word[:-1]

        for character in ascii_lowercase[pi + 1:]:
            if character not in given_word:
                return given_word + character


if __name__ == '__main__':
    from string import ascii_lowercase
    s = input()

    if len(s) < 26:
        result = [s + character for character in ascii_lowercase if character not in s][0]
        print(result)
    elif s == ascii_lowercase[::-1]:
        print(-1)
    else:
        print(determine_next_word(given_word=s))

Submission status: Accepted

Solution code:
S = input()
C = [chr(97+i) for i in range(26)]

if len(S) != 26:
    for c in C:
        if c not in S:
            print(S + c)
            break
else:
    S = list(S)
    for i in range(25, -1, -1):
        if S[i:] != sorted(S[i:], reverse=True):
            Z = sorted(list(filter(lambda x: x > S[i], S[i:])))
            print("".join(S[:i]) + Z[0])
            break
    else:
        print(-1)

Submission status: Accepted

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

d = dict()
d['a'] = 1
d['b'] = 2
d['c'] = 3
d['d'] = 4
d['e'] = 5
d['f'] = 6
d['g'] = 7
d['h'] = 8
d['i'] = 9
d['j'] = 10
d['k'] = 11
d['l'] = 12
d['m'] = 13
d['n'] = 14
d['o'] = 15
d['p'] = 16
d['q'] = 17
d['r'] = 18
d['s'] = 19
d['t'] = 20
d['u'] = 21
d['v'] = 22
d['w'] = 23
d['x'] = 24
d['y'] = 25
d['z'] = 26

e = dict()
e[1] = 'a'
e[2] = 'b'
e[3] = 'c'
e[4] = 'd'
e[5] = 'e'
e[6] = 'f'
e[7] = 'g'
e[8] = 'h'
e[9] = 'i'
e[10] = 'j'
e[11] = 'k'
e[12] = 'l'
e[13] = 'm'
e[14] = 'n'
e[15] = 'o'
e[16] = 'p'
e[17] = 'q'
e[18] = 'r'
e[19] = 's'
e[20] = 't'
e[21] = 'u'
e[22] = 'v'
e[23] = 'w'
e[24] = 'x'
e[25] = 'y'
e[26] = 'z'

appear = [0 for i in range(27)]
string = [0]
for i in range(n):
	string.append(d[s[i]])
	appear[d[s[i]]] += 1

exist = 0
for i in range(1,27):
	if appear[i] == 0:
		exist = i
		break

if exist != 0: #ない文字があった時
	string.append(exist)
	for k in range(1,n+2):
		if k != n+1:
			print(e[string[k]],end='')
		else:
			print(e[string[k]])
else:
	if s == 'zyxwvutsrqponmlkjihgfedcba':
		print('-1')
	else:
		bunki = 26
		i = 26
		while i > 1:
			if string[i-1] > string[i]:
				bunki = i-1
				i += -1
			else:
				break
		memo = []
		for k in range(bunki,27):
			memo.append(string[k])
		memo.sort()
		t = [string[bunki-1]]
		for k in range(len(memo)):
			if t[0] < memo[k]:
				okikae = memo[k]
				break
		string[bunki-1] = okikae
		for h in range(1,bunki):
			if h!= bunki-1:
				print(e[string[h]],end='')
			else:
				print(e[string[h]])

Submission status: Accepted

Solution code:
inp = input()
inp_len = len(inp)
chars = map(ord,list(inp))
dic = {}
an = ""
if inp == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if inp_len == 26:
    for i in range(25):
        if ord(inp[-1 - i]) > ord(inp[-2 - i]):
            an = inp[:-2-i]
            ls = inp[-2-i]
            for char in map(ord,list(inp[-1-i:])):
                dic[char] = "exist"
            break
    for j in range(26):
        if  97 + j in dic and 97+ j  >  ord(ls):
            an =  an + chr(97+j)
            break
else:
    for char in chars:
        dic[char] = "exist"
    for i in range(26):
        if not (97 + i) in dic:
            an =  inp + chr(97+i)
            break
print(an)