qyliang/CodeNanoFix huggingface dataset:
Buggy code:

from string import ascii_lowercase
s = input()

if s == ascii_lowercase[::-1]:
    print(-1)
elif len(s)!=len(ascii_lowercase):
    s1 = set(list(s))
    s2 = set(list(ascii_lowercase))
    w = min(s2-s1)
    print(s+w)
else:
    back = ''
    tmp = ''
    pos = 0
    for i in range(len(s))[::-1]:
        if i==25:
            back += s[i]
            tmp = s[i]
        elif s[i]>tmp:
            back += s[i]
            tmp = s[i]
        else:
            pos = i
            break

    res = s[:pos]
    l = []
    for b in back:
        if b > w:
            l.append(b)
    print(res + min(l))

Fixed code:
from string import ascii_lowercase
s = input()

if s == ascii_lowercase[::-1]:
    print(-1)
elif len(s)!=len(ascii_lowercase):
    s1 = set(list(s))
    s2 = set(list(ascii_lowercase))
    w = min(s2-s1)
    print(s+w)
else:
    back = ''
    tmp = ''
    pos = 0
    for i in range(len(s))[::-1]:
        if i==25:
            back += s[i]
            tmp = s[i]
        elif s[i]>tmp:
            back += s[i]
            tmp = s[i]
        else:
            pos = i
            break
    
    res = s[:pos]
    w = s[pos]
    l = []
    for b in back:
        if b > w:
            l.append(b)
    print(res + min(l))
Buggy code:
s=input()
N=len(s)
alpha="abcdefghijklmnopqrstuvwxyz"
a=[0]*26
for i in range(N):
    for j in range(26):
        if s[i]==alpha[j]:
            a[j]+=1
            break

if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
c=0
lastidx=-1
while True:


    for j in range(lastidx+1,26):
        if a[j]==0 :
            print(s[:c]+alpha[j])
            exit()
    lastidx=alpha.index(s[-1+c])
    a[lastidx] -= 1
    c-=1

Fixed code:
s=input()
N=len(s)
alpha="abcdefghijklmnopqrstuvwxyz"
a=[0]*26
for i in range(N):
    for j in range(26):
        if s[i]==alpha[j]:
            a[j]+=1
            break

if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
c=0
lastidx=-1
while True:
    for j in range(lastidx+1,26):
        if a[j]==0 :
            print(s[:N+c]+alpha[j])
            exit()
    lastidx=alpha.index(s[-1+c])
    a[lastidx] -= 1
    c-=1
Buggy code:
S=list(input())
m=list('abcdefghijklmnopqrstuvwxyz')[::-1]

if len(S)<26:
  for i in m[::-1]
        if i not in S:
            S.append(i)
            break
    print(''.join(S))
else:
    x=S[-1]
    for i in range(24,-1,-1):
        if x>S[i]:
            y=m.index(S[i])
            S=S[:i]
            for j in range(y-1,-1,-1):
                if m[j] not in S:
                    S.append(m[j])
                    break
            break
        else:
            x=S[i]
        
        if i==0:
            print(-1)
            exit()
    print(''.join(S))

Fixed code:
S=list(input())
m=list('abcdefghijklmnopqrstuvwxyz')[::-1]

if len(S)<26:
    for i in m[::-1]:
        if i not in S:
            S.append(i)
            break
    print(''.join(S))
else:
    x=S[-1]
    for i in range(25)[::-1]:
        if x>S[i]:
            y=m.index(S[i])
            S=S[:i]
            for j in range(y)[::-1]:
                if m[j] not in S:
                    S.append(m[j])
                    break
            break
        else:
            x=S[i]
        
        if i==0:
            print(-1)
            exit()
    print(''.join(S))
Buggy code:
import copy
alpha=list("abcdefghijklmnopqrstuvwxyz")
alpha_dict={a:i for i,a in enumerate(alpha)}
alpha = set(alpha)

string = input()

can_use_str = alpha - set(list(string))

if string == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)

elif len(can_use_str)==0:
    k = 0
    for i in reversed(range(25)):
        if string[i] < string[i + 1]:
            k = i
            break
    last = sorted(string[k:])
    for i in last:
        if string[k] < i:
            print(string[:k] + i)

else:
    string+=sorted(list(can_use_str))[0]
    print(string)

Fixed code:
import copy
alpha=list("abcdefghijklmnopqrstuvwxyz")
alpha_dict={a:i for i,a in enumerate(alpha)}
alpha = set(alpha)

string = input()

can_use_str = alpha - set(list(string))

if string == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)

elif len(can_use_str)==0:
    k = 0
    for i in reversed(range(25)):
        if string[i] < string[i + 1]:
            k = i
            break
    last = sorted(string[k:])
    for i in last:
        if string[k] < i:
            print(string[:k] + i)
            break

else:
    string+=sorted(list(can_use_str))[0]
    print(string)
Buggy code:
import string

S = input()

abc = [chr(i) for i in range(97, 97+26)]

if len(S) < 26:
    for s in S:
        abc.remove(s)
    add = abc[0]
    print('wahaha')
    print(S + add)
elif S == ''.join(sorted(abc, reverse=True)):
    print(-1)
else:
    N = len(S)
    for i in range(1, N):
        now = S[N-i]
        fow = S[N-i+1:]
        fows = sorted(list(fow))
        for f in fows:
            if now < f:
                now = f
                break
        if now != S[N-i]:
            ans = S[:N-i] + now
            break
    print(ans)



Fixed code:
import string

S = input()

abc = [chr(i) for i in range(97, 97+26)]

if len(S) < 26:
    for s in S:
        abc.remove(s)
    add = abc[0]
    print(S + add)
elif S == ''.join(sorted(abc, reverse=True)):
    print(-1)
else:
    N = len(S)
    for i in range(1, N):
        now = S[N-i-1]
        fow = S[N-i:]
        fows = sorted(list(fow))
        for f in fows:
            if now < f:
                now = f
                break
        if now != S[N-i-1]:
            ans = S[:N-i-1] + now
            break
    print(ans)
Buggy code:
s = str(input())
list_s = list(s)
alp_list = list("abcdefghijklmnopqrstuvwxyz")
dict_alp = {}
ans = 0
for i in range(26):
    dict_alp[alp_list[i]] = i+1
if len(list_s) == 26:
    i = 25
    while True:
        try:
            if dict_alp[list_s[i]] > dict_alp[list_s[i-1]]:
                list_s = list_s[:i]
                x = list_s.pop()
                break
            else:
                i -= 1
        except IndexError:
            ans = -1
            break
    alp_set = []
    for alp in alp_list:
        if not alp in list_s:
            if dict_alp[alp] > dict_alp[x]:
                alp_set.append(alp)
    alp_set.sort()
    list_s.append(alp_set[0])
else:
    for alp in alp_list:
        if not alp in list_s:
            list_s.append(alp)
            break
if ans == -1:
    print(ans)
else:
    print("".join(list_s))


Fixed code:
s = str(input())
list_s = list(s)
alp_list = list("abcdefghijklmnopqrstuvwxyz")
dict_alp = {}
ans = 0
for i in range(26):
    dict_alp[alp_list[i]] = i+1
if len(list_s) == 26:
    i = 25
    while True:
        try:
            if dict_alp[list_s[i]] > dict_alp[list_s[i-1]]:
                list_s = list_s[:i]
                x = list_s.pop()
                break
            else:
                i -= 1
        except IndexError:
            ans = -1
            break
    if ans != -1:
        alp_set = []
        for alp in alp_list:
            if not alp in list_s:
                if dict_alp[alp] > dict_alp[x]:
                    alp_set.append(alp)
        alp_set.sort()
        list_s.append(alp_set[0])
else:
    for alp in alp_list:
        if not alp in list_s:
            list_s.append(alp)
            break
if ans == -1:
    print(ans)
else:
    print("".join(list_s))
Buggy code:
s = input()
alp = 'abcdefghijklmnopqrstuvwxyz'
if len(s) < 26:
    for a in alp:
        if a not in s:
            print(s + a)
            exit()
else:
    for i in range(25, 0, -1):
        if s[i - 1] < s[i]:
            for j in range(i, 26):
                if s[i - 1] < s[j]:
                    c = s[j]
            print(s[:i-1] + c)
            exit()

Fixed code:
s = input()
alp = 'abcdefghijklmnopqrstuvwxyz'
if len(s) < 26:
    for a in alp:
        if a not in s:
            print(s + a)
            exit()
else:
    for i in range(25, 0, -1):
        if s[i - 1] < s[i]:
            for j in range(i, 26):
                if s[i - 1] < s[j]:
                    c = s[j]
            print(s[:i-1] + c)
            exit()
    print(-1)
Buggy 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)
        exit()
    else:
        for i in range(24,-1,-1):
            for j in range(i,25):
                if s[i]<s[j]:
                    print(s[:i]+s[j])
                    exit()

Fixed 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)
        exit()
    else:
        for i in range(24,-1,-1):
            for j in range(25,i,-1):
                if s[i]<s[j]:
                    print(s[:i]+s[j])
                    exit()
Buggy code:
from collections import deque
from itertools import dropwhile

alphabet = set('abcdefghijklmnopqrstuvwxyz')

if __name__ == '__main__':
    s = input()
    l = [c for c in alphabet - set(s)]
    if l != []:
        print(s + min(l))
        quit(0)

    deq = deque()
    deq.append(s[-1])
    for i in range(24, -1, -1):
        if s[i] < s[i + 1]:
            small = dropwhile(lambda c <= s[i], deq)
            print(s[:i] + small)
            quit(0)
        else:
            deq.append(s[i])

    print(-1)





Fixed code:
from collections import deque
from itertools import dropwhile

alphabet = set('abcdefghijklmnopqrstuvwxyz')

if __name__ == '__main__':
    s = input()
    l = [c for c in alphabet - set(s)]
    if l != []:
        print(s + min(l))
        quit(0)

    deq = deque()
    deq.append(s[-1])
    for i in range(24, -1, -1):
        if s[i] < s[i + 1]:
            small = next(dropwhile(lambda c: c <= s[i], deq))
            print(s[:i] + small)
            quit(0)
        else:
            deq.append(s[i])

    print(-1)
Buggy code:
s = input()
s2 = list(s)
ch = [chr(i) for i in range(97, 97+26)]
flg = 0
for k in ch:
	if not k in s2:
		s += k
		flg = 1
		break

if flg == 1:
	print(s)
	exit(0)
else:
	l = len(s)
	ans = ''
	for i in range(l-1,0,-1):
		if chr((ord(s[i])+1)) != s[i-1]:
			for k in ch[::-1]:
				if k == s[i]:
					break
				if not k in s2[:i]:
					ans = s[:i-1] + k
					print(ans)
					exit(0)
	if s[0] == 'z':
		print(-1)
	else:
		print(chr(ord(s[0])+1))


Fixed code:
s = input()
s2 = list(s)
ch = [chr(i) for i in range(97, 97+26)]
flg = 0
for k in ch:
	if not k in s2:
		s += k
		flg = 1
		break

if flg == 1:
	print(s)
	exit(0)
else:
	l = len(s)
	ans = ''
	for i in range(l-1,0,-1):
		for k in ch:
			if k <= s[i]:
				continue
			elif not k in s2[:i]:
				ans = s[:i] + k
				print(ans)
				exit(0)

	if s[0] == 'z':
		print(-1)
	else:
		print(chr(ord(s[0])+1))
Buggy code:
S = input()
chars = list(map(lambda c: ord(c)-97, S))
if len(chars)!=26:
    chars = sorted(chars)
    i = 0
    while i==chars[i]:
        i += 1
    ans = S + chr(i+97)
else:
    prev = -1
    i = 0
    for c in reversed(chars):
        if prev < c:
            i += 1
            prev = c
        else:
            break
    if i==26:
        ans = -1
    else:
        max_s = chars[-i-1]
        min_c = 27
        for c in chars[-i:]:
            if c>max_s and c<min_c:
                min_c = c
        ans = S[:-i-1] + chr(min_c+97)

print(ans)



Fixed code:
S = input()
chars = list(map(lambda c: ord(c)-97, S))
if len(chars)!=26:
    chars = sorted(chars)
    i = 0
    while i==chars[i]:
        i += 1
        if i==len(chars):
            break
    ans = S + chr(i+97)
else:
    prev = -1
    i = 0
    for c in reversed(chars):
        if prev < c:
            i += 1
            prev = c
        else:
            break
    if i==26:
        ans = -1
    else:
        max_s = chars[-i-1]
        min_c = 27
        for c in chars[-i:]:
            if c>max_s and c<min_c:
                min_c = c
        ans = S[:-i-1] + chr(min_c+97)

print(ans)
Buggy code:
alph={chr(i) for i in range(97,123)}
s=input()
n=len(s)

if n<26:
  print(s+sorted(alph-set(s))[0])
else:
  for i in range(2,27):
    if s[-i]<s[-i+1]:
      break
    if i==26:
      print(-1)
      exit()
for j in sorted(s[i+1:]):
  if j>s[i]:break
print(s[:i]+j)

Fixed code:
a={chr(i) for i in range(97,123)}
s=input()
n=len(s)
if n<26:
  print(s+sorted(a-set(s))[0])
else:
  for i in range(2,27):
    if s[-i]<s[-i+1]:
      break
    if i==26:
      print(-1)
      exit()
  for j in sorted(s[-i+1:]):
    if j>s[-i]:break
  print(s[:-i]+j)
Buggy code:
S = input()
alp = "abcdefghijklmnopqrstuvwxyz"

if len(S) < 26:
  for i in range(len(alp)):
    c = alp[i]
    index = S.find(c)
    if index <0 :
      print(S,c,sep='')
      exit()

stack=[]
c_pre=""
for j in range(len(S)):
  c_now = S[25-j]
  #print(c_now)
  if c_now < c_pre :
    S = S[:-j-1]
    print(S)
    c_min = "z"
    for cx in stack:
      if c_now < cx and cx <= c_min :
        c_min = cx
        #print(c_min)
    print(S,c_min,sep='')
    exit()
  else :
    stack.append(c_now)
    c_pre = c_now

print("-1")    

Fixed code:
S = input()
alp = "abcdefghijklmnopqrstuvwxyz"

if len(S) < 26:
  for i in range(len(alp)):
    c = alp[i]
    index = S.find(c)
    if index <0 :
      print(S,c,sep='')
      exit()

stack=[]
c_pre=""
for j in range(len(S)):
  c_now = S[25-j]
  #print(c_now)
  if c_now < c_pre :
    S = S[:-j-1]
    #print(S)
    c_min = "z"
    for cx in stack:
      if c_now < cx and cx <= c_min :
        c_min = cx
        #print(c_min)
    print(S,c_min,sep='')
    exit()
  else :
    stack.append(c_now)
    c_pre = c_now

print("-1")
Buggy code:

S = input()
def aaa(S):

    Alphabet = "abcdefghijklmnopqrstuvwxyz"

    if len(S) == 26:
        j = 0
        for i in reversed(range(26)):
            while S[i] != Alphabet[j]:
                j = j + 1
                if j == 26:
                   return S[:i] + S[i+1]                
        return -1

    else:
        for i in range(len(Alphabet)):
            j = 0
            while Alphabet[i] != S[j]:
                j += 1
                if j == len(S):
                    return S + Alphabet[i]

print(aaa(S))


Fixed code:

S = input()
def aaa(S):

    Alphabet = "abcdefghijklmnopqrstuvwxyz"

    if len(S) == 26:
        j = 0
        for i in reversed(range(26)):
            while S[i] != Alphabet[j]:
                j = j + 1
                if j == 26:
                    k = 0
                    while S[i] != Alphabet[k]:
                        k += 1
                    while not Alphabet[k] in S[i+1:]:
                        k += 1
                    return S[:i] + Alphabet[k]                
        return -1

    else:
        for i in range(len(Alphabet)):
            j = 0
            while Alphabet[i] != S[j]:
                j += 1
                if j == len(S):
                    return S + Alphabet[i]

print(aaa(S))
Buggy 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])
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:
            memo2=cha[i]
            break
    print(s[:j-1]+memo2)


Fixed 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)
Buggy code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
atoz = [chr(i) for i in range(97,97+26)]

S = input()

if len(S) < 26:
    for a in atoz:
        if not a in S:
            print(S+a)
            break
else:
    right = S[-1]
    for i in reversed(range(len(S))):
        s = S[i]
        if s < right:
            print(S[:i]+right)
            sys.exit()
        else:
            right = max(s,right)
    print(-1)


Fixed code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
atoz = [chr(i) for i in range(97,97+26)]

S = input()

if len(S) < 26:
    for a in atoz:
        if not a in S:
            print(S+a)
            break
else:
    right = S[-1]
    for i in reversed(range(len(S))):
        s = S[i]
        if s < right:
            for j in range(ord(s),ord(s)+26):
                if chr(j) in S[i+1:]:
                    print(S[:i]+chr(j))
                    break
            sys.exit()
        else:
            right = max(s,right)
    print(-1)
Buggy code:
S = input()
T = "abcdefghijklmnopqrstuvwxyz"
if len(S) != 26:
    for i in T:
        if i not in S:
            print(S+i)
            exit()
else:
    if S == "zyxwvutsrqponmlkjihgfedcba" :
        print(-1)
    else:
        dic = []
        kouho = 25
        for i in range(25,-1,-1):
                if S[i] != T[kouho]:
                    dic.append(S[i])
                else:
                    if len(dic) == 0:
                        kouho -= 1
                    if kouho >= -1:
                        for j in range(26):
                            if T[j] == S[0]:
                                print(T[j+1])
                                exit()
                    else:
                        dic = sorted(dic)
                        print(S[0:i-1]+dic[0])
                        exit()


Fixed code:
S = input()
T = "abcdefghijklmnopqrstuvwxyz"
if len(S) != 26:
    for i in T:
        if i not in S:
            print(S+i)
            exit()
else:
    if S == "zyxwvutsrqponmlkjihgfedcba" :
        print(-1)
    else:
        dic = []
        for i in range(25,-1,-1):
            for j in dic:
                if j > S[i]:
                    print(S[:i]+j)
                    exit()
            dic.append(S[i])
            dic = sorted(dic)
Buggy code:
s = input()
if len(s) != 26:
    x = sorted(set(map(chr, range(97, 123)))-set(s))
    print(s+x[0])
else:
    num = list(map(lambda x: ord(x)-ord('a'), s))
    for i in reversed(range(25)):
        L, R = i, i+1
        if num[L] < num[R]:
            tmp = 26
            for r in num[R:]:
                if num[L] < r:
                    tmp = min(tmp, r)
            ans = num[:L]+[tmp]
            print(ans)
            print(''.join(map(lambda x: chr(x+ord('a')), ans)))
            break
    else:
        print(-1)


Fixed code:
s = input()
if len(s) != 26:
    x = sorted(set(map(chr, range(97, 123)))-set(s))
    print(s+x[0])
else:
    num = list(map(lambda x: ord(x)-ord('a'), s))
    for i in reversed(range(25)):
        L, R = i, i+1
        if num[L] < num[R]:
            tmp = 26
            for r in num[R:]:
                if num[L] < r:
                    tmp = min(tmp, r)
            ans = num[:L]+[tmp]
            print(''.join(map(lambda x: chr(x+ord('a')), ans)))
            break
    else:
        print(-1)
Buggy 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(25,0,-1):
    for j in range(i - 1,0,-1):
      if S[i] > S[j]:
        N = sorted(S[j + 1:])
        print(N)
        print(S[:j] + N[0])
        exit()
  else:
    print(-1)


Fixed 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)
Buggy code:
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    k = []
    for i in range(25, 0, -1):
      k.append(s[i])
      if s[i-1] < s[i]:
        for j in sorted(k):
          if j > s[i-1]:
            print(s[:i-1] + j)
            exit()

Fixed code:
al = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    k = []
    for i in range(25, 0, -1):
      k.append(s[i])
      if s[i-1] < s[i]:
        for j in sorted(k):
          if j > s[i-1]:
            print(s[:i-1] + j)
            exit()
Buggy code:
S = list(input())
S = [ord(x) for x in S]
S_exist = sorted(S)
if len(S_exist) != 26:
    for i in range(len(S_exist)):
        if S_exist[i] != i+97:
            S.append(i+97)
            break
    if len(S_exist) == len(S):
        S.append(max(S_exist)+1)
    print("".join(map(str, [chr(x) for x in S])))
else:
    S.reverse()
    if S == S_exist:
        print(-1)
    else:
        flag = 0
        for i in range(25):
            if S[i] > S[i+1]:
                a = S[i+1]
                for j in range(len(S[:i+2])-1, -1, -1):
                    if S[j] > a:
                        S = [S[j]] + S[i+2:]
                        flag = 1
                        break
            if flag == 1:
                break
        S.reverse()
        print("".join(map(str, [chr(x) for x in S])))


Fixed code:
S = list(input())
S = [ord(x) for x in S]
S_exist = sorted(S)
if len(S_exist) != 26:
    for i in range(len(S_exist)):
        if S_exist[i] != i+97:
            S.append(i+97)
            break
    if len(S_exist) == len(S):
        S.append(max(S_exist)+1)
    print("".join(map(str, [chr(x) for x in S])))
else:
    S.reverse()
    if S == S_exist:
        print(-1)
    else:
        flag = 0
        for i in range(25):
            if S[i] > S[i+1]:
                a = S[i+1]
                for j in sorted(S[:i+2]):
                    if j > a:
                        S = [j] + S[i+2:]
                        flag = 1
                        break
            if flag == 1:
                break
        S.reverse()
        print("".join(map(str, [chr(x) for x in S])))
Buggy code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    s = readline().decode('utf-8')
    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()

Fixed 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()