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: Runtime Error

Solution code:
S=list(input())
abc=list("abcdefghijklmnopqrstuvwxyz")
idx={}
for i in range(26):
    idx[abc[i]]=i
D=[0]*26
for s in S:
    D[idx[s]]+=1

if len(S)!=26:
    for i in range(26):
        if D[idx[abc[i]]]==0:
            ans="".join(S)+abc[i]
            break
        
else:
    ans=-1
    now=S[-1]
    for i in range(len(S)-1, 0,-1):
        if idx[S[i]]> idx[S[i-1]]:
            now=sorted(list(now))
            #print(now)
            for j,n in enumerate(now):
                if idx[n]>idx[S[i-1]]:
                    pre=n

                break

            ans="".join(S[:i-1])+pre
            break


        else:
            now=str(now)
            now+=S[i-1]

print(ans)

Submission status: Runtime Error

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

if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
    
alp_d = {chr(ord('a') + i): False for i in range(26)}
if n < 26:
    for si in s:
        alp_d[si] = True
    for k,v in alp_d.items():
        if not v:
            print(s+k)
            exit()

else:
    alps = 'abcdefghijklmnopqrstuvwxyz'
    ans = []
    for si,a in zip(s,alps):
        if si == a:
            ans.append(si)
        else:
            ans[-1] = a
            print(''.join(ans))
            exit()

    # alps = 'abcdefghijklnmopqrstuvwxyz'

Submission status: Runtime Error

Solution code:
S = list(input())
ic = ""
for i in range(97, 123):
  if (chr(i) not in S):
    ic = chr(i)
    break
if ic != "":
  print("".join(S) + ic)
else:
  h = S[0]
  for i in range(1,len(S)):
    if h < S[i]:
      h = S[i]
    else:
      ii = i
      break
  print(ii)

Submission status: Runtime Error

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

if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
    
alp_d = {chr(ord('a') + i): False for i in range(26)}
if n < 26:
    for si in s:
        alp_d[si] = True
    for k,v in alp_d.items():
        if not v:
            print(s+k)
            exit()

else:
    alps = 'abcdefghijklmnopqrstuvwxyz'
    ans = []
    for si,a in zip(s,alps):
        if si == a:
            ans.append(si)
        else:
            ans[-1] = a
            print(''.join(ans))
            exit()

Submission status: Runtime Error

Solution code:
from collections import defaultdict
s = input()

n = len(s)
a_z = "abcdefghijklmnopqrstuvwxyz"

if n != 26:
    a_dict = defaultdict(bool)
    for a in s:
        a_dict[a] = True
    for a in a_z:
        if not a_dict[a]:
            last = a
            break
    print(s + last)
else:
    later_list = []
    for i in range(1, n)[::-1]:
        later = s[i]
        next_later = s[i-1]

        if a_z.find(next_later) > a_z.find(later):
            later_list.append(later)
        else:
            cut = i-1
            break
    if cut == 0:
        print(-1)
    else:
        later_list.sort()
        idx = a_z.find(s[cut])
        for later in later_list:
            if a_z.find(later) > idx:
                this = later
                break
        print(s[:cut] + this)

Submission status: Runtime Error

Solution code:
s = list(input())
l = [chr(i) for i in range(ord("a"), ord("z") + 1)]
if len(s) == 26:
    if s == reversed(l):
        print(-1)
        exit()
    else:
        fi = False
        for i in range(26)[-2::-1]:
            if fi:
                break
            for j in range(25, i, -1):
                if s[i] < s[j]:
                    s[i] = s[j]
                    s = s[:i+1]
                    fi = True
                    break
for i in range(26)[-1::-1]:
    if l[i] in s:
        del l[i]
s.append(l[0])
print("".join(s))

Submission status: Runtime Error

Solution code:
s = input()
if s == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit(0)
l = list('abcdefghijklmnopqrstuvwxyz')
for i in range(26):
    if l[i] != s[i]:
        print(s[:i-1] + l[i])
        exit(0)

Submission status: Runtime Error

Solution code:
s=input()
al='abcdefghijklmnopqrstuvwxyz'
if len(s)<=25:
  al=list(al)
  for i in range(len(s)):
    al.remove(s[i])
  print(s+al[0])
elif s=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
else:
  flg=-1
  for i in range(len(s)):
    if flg==-1 and s[i]!=al[i]:
      flg=i
    elif s[i]!=al[i]:
      break
  al2=list(al[i+1:])
  for j in range(i+1,len(s)):
    if s[j]==al[j]:
      al2.remove(s[j])
  if len(al2)==0:
    print(s[:i+1]+al[i+2])
  else:
    print(s[:i+1]+al2[0])

Submission status: Runtime Error

Solution code:
import collections
s = input()
alphabet = "abcdefghijklmnopqrstuvwxyz"
dif = sorted(list(set(alphabet) - set(s)))
if len(dif):
    print(s + dif[0])
else:
    if reversed(alphabet) == s:
        print(-1)
    else:
        tmp = [s]
        for i in range(25):
            if s[i] == alphabet[i]:
                continue
            else:
                dif = sorted(list(set(alphabet) - set(s[:i])))
                tmp.append(s[:i-1] + dif[0])
        tmp.sort()
        print(tmp[tmp.index(s)+1])

Submission status: Runtime Error

Solution code:
import collections
s = input()
alphabet = "abcdefghijklmnopqrstuvwxyz"
dif = sorted(list(set(alphabet) - set(s)))
if len(dif):
    print(s + dif[0])
else:
    if alphabet[::-1] == s:
        print(-1)
    else:
        tmp = [s]
        for i in range(25):
            if s[i] == alphabet[i]:
                continue
            else:
                tmp.append(s[:i-1] + alphabet[i])
        tmp.sort()
        print(tmp[tmp.index(s)+1])

Submission status: Runtime Error

Solution code:
import collections
s = input()
alphabet = "abcdefghijklmnopqrstuvwxyz"
dif = sorted(list(set(alphabet) - set(s)))
if len(s) > 26:
    print(-1)
elif 0 < len(dif):
    if len(s) == len(set(s)):
        print(s + dif[0])
    else:
        print(-1)
else:
    if alphabet[::-1] == s:
        print(-1)
    else:
        tmp = [s]
        for i in range(25):
            if s[i] == alphabet[i]:
                continue
            else:
                tmp.append(s[:i-1] + alphabet[i])
        tmp.sort()
        print(tmp[tmp.index(s)+1])

Submission status: Runtime Error

Solution code:
import collections
s = input()
alphabet = "abcdefghijknmlopqrstuvwxyz"
dif = sorted(list(set(alphabet) - set(s)))
if len(dif) <= 26:
    if len(s) == len(set(s)):
        print(s + dif[0])
    else:
        print(-1)
elif len(s) > 26:
    print(-1)
else:
    if reversed(alphabet) == s:
        print(-1)
    else:
        tmp = [s]
        for i in range(25):
            if s[i] == alphabet[i]:
                continue
            else:
                tmp.append(s[:i] + alphabet[i])
        tmp.sort()
        print(tmp[tmp.index(s)-1])

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int64_t MOD=1000000007;
const long long INF = 1LL<<60;

int main() {
  string S; cin>>S;
  int T=S.size();
  if(T<26){
    rep(i,26){
      char s;
      s=(i+97);
      bool id=true;
      rep(j,T){
        if(s==S.at(j))id=false;
      }
      if(id){
        S.push_back(s);
        cout<<S;
        break;
      }
    }
  }
  else{
    int i=T-2;
    for(;i>=0;i--){
      if(S.at(i)<S.at(i+1)) break;
    }
    if(i==0) cout<<-1;
    else{
      S.at(i)++;
      rep(j,i+1) cout<<S.at(j);
    }
  }   
}

Submission status: Runtime Error

Solution code:
s = list(input())
arufa = ["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 s:
    arufa.remove(i)
if arufa == []:
    dum = sorted(s,reverse=1)
    if dum == s:
        print(-1)
    else:
        arufa.append(s.pop(-1))
        for i in range(27):
            arufa.sort()
            if s[-1] < arufa[0]:
                s.pop(-1)
                s.append(arufa[0])
                break
            #for j in range(len(arufa)):
            #    if s[-1] < arufa[i]:
            #        s.append(arufa[0])
            #        print("".join(s))
            #        exit()
            arufa.append(s.pop(-1))
        print("".join(s))

else:
    s.append(arufa[0])
    print("".join(s))

Submission status: Runtime Error

Solution code:
s = list(input())
arufa = ["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 s:
    arufa.remove(i)
if arufa == []:
    dum = sorted(s,reverse=1)
    if dum == s:
        print(-1)
    else:
        arufa.append(s.pop(-1))
        for i in range(26):
            arufa.sort()
            if s[-1] < arufa[0]:
                s.pop(-1)
                s.append(arufa[0])
                break
            #for j in range(len(arufa)):
            #    if s[-1] < arufa[i]:
            #        s.append(arufa[0])
            #        print("".join(s))
            #        exit()
            arufa.append(s.pop(-1))
        print("".join(s))

else:
    s.append(arufa[0])
    print("".join(s))

Submission status: Runtime Error

Solution code:
s = list(input())
arufa = ["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 s:
    arufa.remove(i)
if arufa == []:
    dum = sorted(s,reverse=1)
    if dum == s:
        print(-1)
    else:
        arufa.append(s.pop(-1))
        for i in range(26):
            arufa.sort()
            if s[-1] < arufa[0]:
                s.append(arufa[0])
                break
            #for j in range(len(arufa)):
            #    if s[-1] < arufa[i]:
            #        s.append(arufa[0])
            #        print("".join(s))
            #        exit()
            arufa.append(s.pop(-1))
        print("".join(s))

else:
    s.append(arufa[0])
    print("".join(s))

Submission status: Runtime Error

Solution code:
S = str(input())
N = len(S)
L = list(S)
L = set(L)
if N != 26: #含まないものがある
  for i in range(26):
    temp = chr(i+97)
    if temp not in L:
      ans = S+temp
      print(ans)
      exit()
M = []
for i in range(25):
  now = 25-i
  if S[now] > S[now-1]:
    ans = S[:now-1]+min(M)
    print(ans)
    exit()
  M.append(S[now])
print(-1)

Submission status: Runtime Error

Solution code:
s=str(input())
t=["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 len(s)!=26:
  for i in range(26):
    if t[i] not in s:
      s.append(t[i])
      break
  print("".join(s))
else:
  if "".join(s)=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    a=[]
    for i in range(26):
      a.append(s[25-i])
      for j in a:
        if s<s[:25-i]+j:
          print(s[:25-i]+j)
          exit()

Submission status: Runtime Error

Solution code:
s=str(input())
s=list(s)
t=["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 len(s)!=26:
  for i in range(26):
    if t[i] not in s:
      s.append(t[i])
      break
  print("".join(s))
else:
  if "".join(s)=="abcdefghijklmnopqrstuvwxyz":
    print("abcdefghijklmnopqrstuvwxzy")
  elif "".join(s)=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    ans=[]
    for i in range(26):
      if s[i]==t[i]:
        ans.append(s[i])
      else:
        if len(ans)==1:
          ans.append(t[i])
        else:
          del ans[len(ans)-1]
          ans.append(t[i])
          break
    print("".join(ans))

Submission status: Runtime Error

Solution code:
s=str(input())
s=list(s)
t=["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 len(s)!=26:
  for i in range(26):
    if t[i] not in s:
      s.append(t[i])
      break
  print("".join(s))
else:
  if "".join(s)=="abcdefghijklmnopqrstuvwxyz":
    print("abcdefghijklmnopqrstuvwxzy")
  elif "".join(s)=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    ans=[]
    for i in range(26):
      if s[i]==t[i]:
        ans.append(s[i])
      else:
        del ans[len(ans)-1]
        ans.append(t[i])
        break
    print("".join(ans))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[9]:


S = input()


# In[18]:


ZtoA = list(reversed("abcdefghijklmnopqrstuvwxyz"))
diff = sorted(list(set(ZtoA)^set(S)))
if len(S) < 26:
    print(S+diff[0])
else:
    if S == ZtoA:
        print(-1)
    else:
        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)


# In[ ]:

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

S = list(input())
check = [0]*26
if len(S)==26:
    else:
        end = S[-1]
        for i in range(24, -1, -1):
            if ord(S[i])<ord(end):
                S[i] = end
                print(''.join(S[:i+1]))
                break
        else:
            if S[0]=='z':
                print(-1)
            else:
                print(chr(ord(S[0])+1))
else:
    for s in S:
        check[ord(s)-97] = 1
    for i in range(26):
        if not check[i]:
            S.append(chr(i+97))
            break
    print(''.join(S))

Submission status: Runtime Error

Solution code:
from string import ascii_lowercase

S = input()

used = set(list(S))
alph = set(ascii_lowercase)

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

if len(used) < 26:
    ans = S + min(used ^ alph)
    print(ans)
    exit()
else:
    S = list(S)
    while len(S) > 0:
        last = S.pop()
        second = S.pop()
        if last < second:
            S.append(last)
        else:
            S.append(last)
            print(*S, sep="")
            exit()

Submission status: Runtime Error

Solution code:
def resolve():
    '''
    code here
    '''
    import string
    S = input()

    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        if len(S) < 26:
            temp_str = [item for item in string.ascii_lowercase]
            for item in S:
                temp_str.remove(item)
            print(S+temp_str[0])
            
        else:
            res_s = S[0]
            temp_str = [item for item in string.ascii_lowercase]
            temp_str.remove(S[0])
            for i in range(1,26):
                temp_str.remove(S[i])
                if ord(S[i-1]) + 1 < ord(S[i]):
                    break
                res_s += S[i]


            print(res_s[:-1] + temp_str[0])


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
def resolve():
    '''
    code here
    '''
    import string
    S = input()

    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        if len(S) < 26:
            temp_str = [item for item in string.ascii_lowercase]
            for item in S:
                temp_str.remove(item)
            print(S+temp_str[0])
            
        else:
            temp_str = [item for item in string.ascii_lowercase]
            temp_str.remove(S[0])
            for i in range(1,26):
                temp_str.remove(S[i])
                if ord(S[i-1])+1 >= ord(S[i]):
                    pass
                else:
                    temp_index = i
                    break

            temp_index = i

            print(S[:temp_index-1] + temp_str[0])


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
def resolve():
    '''
    code here
    '''
    import string
    S = input()

    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        if len(S) < 26:
            temp_str = [item for item in string.ascii_lowercase]
            for item in S:
                temp_str.remove(item)
            print(S+temp_str[0])
            
        else:
            temp_str = [item for item in string.ascii_lowercase]
            temp_str.remove(S[0])
            for i in range(1,26):
                if ord(S[i-1])+1 >= ord(S[i]):
                    temp_str.remove(S[i])
                else:
                    temp_index = i
                    break
                
            temp_index = i

            print(S[:temp_index-1] + temp_str[0])


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
def resolve():
    '''
    code here
    '''
    import string
    S = input()

    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        if len(S) < 26:
            temp_str = [item for item in string.ascii_lowercase]
            for item in S:
                temp_str.remove(item)
            print(S+temp_str[0])
            
        else:
            temp_str = [item for item in string.ascii_lowercase]
            temp_str.remove(S[0])
            for i in range(1,26):
                if chr(ord(S[i-1])+1) >= S[i]:
                    temp_str.remove(S[i])
                else:
                    temp_index = i
                    break

            print(S[:temp_index-1] + temp_str[0])





if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
s = list(input())
lis = [0]*26
for t in s:
    lis[ord(t)-ord("a")] = 1

q = -1
for i in range(26):
    if lis[i] == 0:
        q = i
        break
if len(s) != 26:
    s.append(chr(ord("a") + q))
if q == -1:
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        lis2 = [0]*26
        for i in range(26):
            lis[ord(s[i])-ord("a")] = 1
            if i == 0:
                r = ord(s[i])-ord("a")
            else:
                if ord(s[i])-ord("a") != r+1:
                    s[i-1] = chr(ord("a") + r+1)
                    p = i
                    break
                else:
                    r += 1
            if r == 25:
                r = 0
        print("".join(s[:p]))
        
                    
        
else:
    print("".join(s))

Submission status: Runtime Error

Solution code:
s = list(input())
lis = [0]*26
for t in s:
    lis[ord(t)-ord("a")] = 1
print(lis)
q = -1
for i in range(26):
    if lis[i] == 0:
        q = i
        break
s.append(chr(ord("a") + q))
if q == -1:
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        lis2 = [0]*26
        for i in range(26):
            lis[ord(s[i])-ord("a")] = 1
            if i == 0:
                r = ord(s[i])-ord("a")
            else:
                if ord(s[i])-ord("a") != r+1:
                    s[i+1] = chr(r+1)
                    p = i
                    break
                else:
                    r += 1
        print("".join(s[:p]))
        
                    
        
else:
    print("".join(s))

Submission status: Runtime Error

Solution code:
import collections
s = input()


cnt = collections.Counter(s)
used = set(cnt.keys())
c = -1
if len(used) < 26:
    for i in range(26):
        if not chr(i+ord("a")) in used:
            c = chr(i+ord("a"))
            break
if c != -1:
    print(s+c)
else:
    i = 25
    while s[i-1] > s[i]:
        i -= 1
        lused = sorted(s[i-1:])
        print(s[:i-1]+lused[lused.index(s[i-1])+1])
        break
    else:
        print("-1")

Submission status: Runtime Error

Solution code:
from collections import defaultdict
d = defaultdict(int)
a = ['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']

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

if len(S) < 26:
    for x in S:
        d[x] += 1
    for x in a:
        if d[x] == 0:
            print(S+x)
            exit()
else:
    b = 25
    for i in range(24, 0, -1):
        if S[i] > S[i+1]:
            b = i-1
            break
    ss = sorted(list(S[b-1:]))
    t = S[b-1]
    print(S[:b-1] + ss[ss.index(t)+1])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
d = defaultdict(int)
a = ['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']

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

if len(S) < 26:
    for x in S:
        d[x] += 1
    for x in a:
        if d[x] == 0:
            print(S+x)
            exit()
else:
    index = 25
    for i in range(25):
        if S[i] > S[i+1]:
            index = i
            break
    ss = sorted(S[index-1:])
    print(S[:index-1] + ss[ss.index(S[i-1])+1])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
d = defaultdict(int)
a = ['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']

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

if len(S) < 26:
    for x in S:
        d[x] += 1
    for x in a:
        if d[x] == 0:
            print(S+x)
            exit()
else:
    index = 25
    for i in range(25):
        if S[i] > S[i+1]:
            index = i
            break
    ss = sorted(S[index-1:])
    print(S[:index-1] + ss[1])

Submission status: Runtime Error

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  exit()
if len(S) != 26:
  L = [0]*26
  for i in range(len(S)):
    L[ord(S[i])-97] = 1
  for i in range(26):
    if L[i] == 0:
      print(S + chr(97+i))
      exit()
else:
  B = []
  for i in reversed(range(26)):
    if ord(S[i]) - ord(S[i-1]) > 1:
      U = i
      M = i-1
      B += [ord(S[i])]
      break
    else:
      B += [ord(S[i])]
  B.sort()
  print(S[:M] + chr(B[0]))

Submission status: Runtime Error

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

if L != 26:
  # まだ使ってないもので、最大ものもを最後につける
  USED = [False] * 26
  for s in S:
    USED[ord(s) - 97] = True
  
  for i in range(26):
    if not USED[i]:
      S.append(chr(97+i))
      break
  
  print("".join(S))
else:
  # 26文字ある場合
  prev = -1
  k = -1
  USED = []
  for i in range(26)[::-1]:
    d = ord(S[i]) - 97
    if prev < d:
      prev = d
      USED.append(S[i])
    else:
      k = i
      break
  
  if K == -1:
    print(-1)
    exit()
  
  S = S[:k] + min(USED)
  print("".join(S))

Submission status: Runtime Error

Solution code:
S = input()
K = list(S)
L = set(K)
print(L)
if len(L) == len(S):
    if len(L) == 26:
        if S == "zyxwvutsrqponmlkjihgfedcba":
            print(-1)
        else:
            ans = S[0]
            for i in range(26):
                if S[i] < S[i+1]:
                    ans += S[i+1]
                else:
                    print(ans)
                    break
    else:
        for i in range(26):
            if chr(97+i) not in L:
                print(S+chr(97+i))
                break
else:
    print(-1)