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

Solution (Python):
import itertools


def main():
    a = 'abcdefghijklmnopqrstuvwxyz'
    s = input()

    if len(s) < 26:
        for v in s:
            a = a.replace(v, '')
        print(s + a[0])
    elif s == a[::-1]:
        print(-1)
    else:
        for i in (range(25))[::-1]:
            if s[i] < s[i+1]:
                for j in range(i+1, len(s))[::-1]:
                    if s[i] < s[j]:
                        print(s[:i]+s[j])
                        exit()


def input_list():
    return list(map(int, input().split()))

if __name__ == "__main__":
    main()

Solution (Python):
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)

Solution (Python):
S = list(input())
N = len(S)
al = [chr(ord('a') + i) for i in range(26)]
if N!=26:
    D = dict(zip(al,[0]*26))
    cand = []
    for i in range(N):
        D[S[i]]+=1
    for key in D.keys():
        if D[key]==0:
            cand.append(key)
    cand.sort()
    S.append(cand[0])
    print("".join(S))
else:
    if S==list(reversed(al)):
        print(-1)
    else:
        cand = []
        for i in range(26):
            if S[25-i-1]>S[25-i]:
                cand.append(S[25-i])
                continue
            else:
                change_ind = 25-i
                cand.append(S[25-i])
                break
        cand.sort()
        T = S[0:change_ind]
        i = 0
        while True:
            if T[-1]<cand[i]:
                T[-1]=cand[i]
                break
            i+=1
        print("".join(T))

Solution (Python):
import sys
input = sys.stdin.readline

S = input()

alphabets = set(chr(ord('a')+x) for x in range(26))

def F(S,alphabets):
    if len(S) == 0:
        return -1
    T = F(S[1:],alphabets-set([S[0]]))
    if T == -1:
        larger = [x for x in alphabets if x > S[0]]
        if not larger:
            return -1
        return min(larger)
    return S[0] + T

answer = F(S,alphabets)
print(answer)

Solution (Python):






S = input()







abc = "abcdefghijklmnopqrstuvwxyz"
dic = {d: 0 for d in abc}

for s in S:
    dic[s] += 1

for k in dic:
    if dic[k] == 0:
        print(S + k)
        exit()

li = []
for i in range(1, len(S) + 1):
    s = S[-i]
    for l in li:
        if s < l:
            print(S[: len(S) - i] + l)
            exit()
    li.append(s)
    li.sort()

print(-1)

Solution (Python):
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))

Solution (Python):
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))

            for j,n in enumerate(now):
                if idx[n]>idx[S[i-1]]:
                    pre=n
                    ans="".join(S[:i-1])+pre
                    print(ans)
                    exit()



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

print(ans)

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter
import heapq


import bisect
s=input()
res=[0]*26
n=len(s)
if n!=26:
    for c in s:
        res[ord(c)-ord('a')]=1
    for i in range(26):
        if res[i]==0:
            ans=i
            break
    print(s+chr(ans+ord('a')))
else:
    s=list(s)
    res=[]
    for i in range(n-1):
        if ord(s[n-1-i])-ord(s[n-2-i])>0:
            idx=ord(s[n-2-i])
            res.sort()
            if not res:
              s[n-1-i],s[n-2-i]=s[n-2-i],s[n-1-i]
              print("".join(s[:n-1-i]))
              exit()
            ch=bisect.bisect_right(res,idx)
            if ch==len(res):
                res=[chr(i) for i in res]
                res.append(s[n-2-i])
                ans=[]
                for j in range(n):
                    if j!=n-2-i:
                        ans.append(s[j])
                    else:
                        ans.append(s[n-1-i])
                        break
                print(''.join(ans))
            else:
                minc=chr(res[ch])
                ans=[]
                for j in range(n):
                    if j!=n-2-i:
                        ans.append(s[j])
                    else:
                        ans.append(minc)
                        break
                print(''.join(ans))
            break
        else:
            res.append(ord(s[n-1-i]))
    else:
        print(-1)

Solution (Python):
s = input()
d_memo = [chr(i) for i in range(97, 97+26)]
s_memo = set(list(s))

def next_c(sub_s, b_c):
    c = None
    for d in d_memo:
        if d not in set(list(sub_s)):
            if d > b_c:
                c = d
                break
    return c

if len(s) < len(d_memo):
    c = next_c(s, '')
    ans = s+c
else:
    b_c = ''
    ans = None
    for i in range(len(s)+1):
        c = next_c(s[:len(s)-i], b_c)
        if c is not None:
            ans = s[:len(s)-i]+c
            break
        else:
            b_c = s[len(s)-i-1]

if ans is None:
    print('-1')
else:
    print(ans)

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

now="zz"
for i in range(n-1,-1,-1):
    if s[i-1]<s[i]:
        if i==1:
            print(chr(ord(s[0])+1));exit()
        qq=s[i]
        for nowc in s[i+1:]:
            if nowc> s[i-1] and qq>nowc:
                qq=nowc
        print(s[:i-1]+qq);exit()

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = S()
    if len(s)<26:
        for char in string.ascii_lowercase:
            if char not in s:
                print(s+char)
                break
    else:
        for i in range(25, -1, -1):
            ex = s[i:]
            late = list(ex)
            late.sort(reverse=True)
            late = "".join(late)
            if ex != late:
                index = late.index(s[i])
                ans = s[:i] + late[index-1]
                print(ans)
                break
        else:
            print(-1)
    

main()

Solution (Python):
s = list(input())
m = list('zyxwvutsrqponmlkjihgfedcba')
if len(s) < 26:
    for i in m[::-1]:
        if not i 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 not m[j] in s:
                    s.append(m[j])
                    break
            break
        else:
            x = s[i]
        if i == 0:
            print(-1)
            exit(0)
    print(''.join(s))

Solution (Python):
import sys, math
from functools import lru_cache
from collections import defaultdict
from decimal import Decimal
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    S = input()
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
        return

    l = [0]*26
    for c in S:
        l[ord(c)-ord('a')] += 1

    if len(S) < 26:
        i = l.index(0)
        print(S+chr(i+ord('a')))
        return

    for i in range(len(S)-1, 0, -1):
        if S[i-1] < S[i]:
            break

    c = min(S[j] for j in range(i, len(S)) if S[j] > S[i-1])
    print(S[:i-1]+c)

if __name__ == '__main__':
    main()

Solution (Python):
s = list(input())
m = list('zyxwvutsrqponmlkjihgfedcba')
if len(s) < 26:
    for i in m[::-1]:
        if not i 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 not m[j] in s:
                    s.append(m[j])
                    break
            break
        else:
            x = s[i]
        if i == 0:
            print(-1)
            exit(0)
    print(''.join(s))

Solution (Python):
s = input(); a = []; b = [0]*27; d = []; t = ""
for i in range(len(s)): a.append(ord(s[i])); b[a[i]-97] = 1
if s == "zyxwvutsrqponmlkjihgfedcba": print(-1)
else:
    for i in range(27):
        if b[i] == 0: c = i; break
    if c != 26: a.append(c+97)
    else:
        for _ in range(24):
            if a[-2] > a[-1]: d.append(a.pop())
            else: d.append(a.pop()); d.sort(); break
        else: d.append(a.pop()); d.sort()
        for i in range(len(d)):
            if a[-1] < d[i]: a.pop(); a.append(d[i]); break
    for i in range(len(a)): t += chr(a[i])
    print(t)

Solution (Python):
s = input(); a = []; b = [0]*27; t = ""
for i in range(len(s)): a.append(ord(s[i])); b[a[i]-97] = 1
if s == "zyxwvutsrqponmlkjihgfedcba": print(-1)
else:
    for i in range(27):
        if b[i] == 0: c = i; break
    if c != 26:
        a.append(c+97)
        for i in range(len(a)): t += chr(a[i])
        print(t)
    else:
        d = []
        for _ in range(24):
            if a[-2] > a[-1]: d.append(a.pop())
            else:
                d.append(a.pop()); d.sort()
                break
        else: d.append(a.pop()); d.sort()
        for i in range(len(d)):
            if a[-1] < d[i]: a.pop(); a.append(d[i]); break
        for i in range(len(a)): t += chr(a[i])
        print(t)

Solution (Python):
import sys

input = sys.stdin.readline
S =  input()
S = S.replace("\n","")
s_list = list(S)
alpha_list = [chr(i) for i in range(97, 97+26)]
flag_list = [False for _ in range(26)]


if len(s_list) == 26:
    for i in range(25, 0, -1):
        if s_list[i - 1] < s_list[i]:
            c = 'ω'
            for j in range(i, 26):
                if s_list[i - 1] < s_list[j]:
                    c = min(c, s_list[j])
            print(S[:i-1] + c)
            exit(0)
    print(-1)

else:
    for i,alpha in enumerate(alpha_list):
        if alpha in s_list:
            flag_list[i] = True
    for i,flag in enumerate(flag_list):
        if not flag:
            print(S + alpha_list[i])
            break

Solution (Python):
S = input()
if len(S) == 26:
    done = set()
    for i, s in enumerate(S[::-1]):
        s = ord(s)
        if done:
            if max(done) < s:
                done.add(s)
                if i == 25:
                    print(-1)
            elif i != 25:
                l = 30
                for d in done:
                    if l > abs(d - s) and s < d:
                        x = d
                        l = d - s
                print(S[: -(i + 1)] + chr(x))
                break
            else:
                print(chr(ord(S[0]) + 1))
        else:
            done.add(s)
else:
    for i in range(ord("a"), ord("z") + 1):
        if not (chr(i) in S):
            print(S + chr(i))
            break

Solution (Python):
S = input()
ans = -1
for i in range(ord('a'), ord('z') + 1):
    if chr(i) in S:
        continue
    else:
        ans = S + chr(i)
        print(ans)
        exit()
R = set()
for i, s in enumerate(S[::-1], 1):
    for j in range(ord(s)+1, ord('z')+1):
        if chr(j) in R:
            ans = S[:-i] + chr(j)
            print(ans)
            exit()
    R.add(s)
print(ans)

Solution (Python):
s = input()

used = [0] * 26

for x in s:
    idx = ord(x) % 26
    used[idx] += 1

if len(s) < 26:
    for i in range(97, 97+26+1):
        if not used[i%26]:
            print(s+chr(i))
            exit()

decrease_point = 0
for i in range(len(s)-1):
    if s[i] < s[i+1]:
        decrease_point = i + 1

if decrease_point == 0:
    print(-1)
    exit()

add_chr = s[decrease_point]
for x in s[decrease_point:]:
    if x > s[decrease_point-1] and x < add_chr:
        add_chr = x

print(s[:decrease_point-1] + add_chr)

Solution (Python):


import sys



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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    S = input()
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    if len(S) < 26:
        for i in range(ord("a"), ord("z") + 1):
            if chr(i) not in S:
                print(S + chr(i))
                return
    elif len(S) == 26:
        for i in range(25)[::-1]:
            c = S[i]
            ss = S[:i]
            for j in range(ord(c) + 1, ord("z") + 1):
                cc = chr(j)
                if cc not in ss:
                    print(ss + cc)
                    return
        print(-1)

    return


if __name__ == "__main__":
    main()

Solution (Python):
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))

            for j,n in enumerate(now):
                if idx[n]>idx[S[i-1]]:
                    pre=n
                    ans="".join(S[:i-1])+pre
                    print(ans)
                    exit()



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

print(ans)

Solution (Python):

S = input()
alp = "abcdefghijklmnopqrstuvwxyz"
def h(string, least = 'Z'):
  for c in alp:
    if c not in string:
      if c <= least:
        continue
      return string + c
  if string == "":
    return "-1"
  return h(string[:-1], string[-1])
print(h(S))

Solution (Python):
S = input()
N = len(S)
for c in "abcdefghijklmnopqrstuvwxyz":
    if c not in S:
        print(S + c)
        exit()

for i in range(N - 1, -1, -1):
    cur = ord(S[i]) - ord("a")
    for diff in range(1, 26):
        if cur + diff >= 26:
            continue
        nxt_c = chr(cur + diff + ord("a"))
        if nxt_c in S[:i]:
            continue
        print(S[:i] + nxt_c)
        exit()
else:
    print(-1)

Solution (Python):
S=input()

A="abcdefghijklmnopqrstuvwxyz"
if S=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(S)!=26:
    A=list(A)
    for i in range(len(S)):
        A.remove(S[i])
    print(S+A[0])
else:
    a=A.index(S[25])
    for i in range(24,-1,-1):
        b=A.index(S[i])
        if a>b:
            break
        a=b

    for j in range(b+1,26):
        if not A[j] in S[:i]:
            print(S[:i]+A[j])
            break

Solution (Python):
al=[chr(ord('a') + i) for i in range(26)]
reval=al[::-1]
s=list(input())
for si in s:
    al.remove(si)

if s==reval:
    print(-1)
elif len(al)!=0:
    print(''.join(s)+al[0])
else:
    k=0
    for i in range(26):
        if ord(s[25-i])>ord(s[24-i]):
            k=i
            break
    al=[chr(ord('a') + i) for i in range(26)]
    for i in range(24-k):
        al.remove(s[i])
    x=''
    for j in al:
        if j>s[24-k]:
            x=j
            break
    print(''.join(s[:24-k])+x)

Solution (Python):
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]:
            c = 'ω'
            for j in range(i, 26):
                if s[i - 1] < s[j]:
                    c = min(c, s[j])
            print(s[:i-1] + c)
            exit()

    print(-1)

Solution (Python):
from string import ascii_lowercase
S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()
if len(S) < 26:
    for a in ascii_lowercase:
        if a not in S:
            print(S + a)
            exit()
can = []
for i in range(25, 0, -1):
    can.append(S[i])
    if S[i - 1] < S[i]:
        for c in sorted(can):
            if S[i - 1] < c:
                print(S[:i - 1] + c)
                exit()

Solution (Python):
S = input()
alphabets = set(chr(ord("a") + x) for x in range(26))

def F_short(S):
  se = alphabets - set(S)
  x = min(se)
  return S + x

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

L = len(S)
ans = F_short(S) if L < 26 else F_long(S)
print(ans)

Solution (Python):
S = input()
if len(S)<26:
    for c in map(chr, range(97,123)):
        if c not in S:
            print(S+c)
            exit()
for i in range(26):
    for c in map(chr, range(ord(S[25-i])+1,ord('z')+1)):
            if c not in S[0:25-i]:
                print(S[0:25-i]+c)
                exit()
print(-1)

Solution (Python):
from collections import Counter
s=input()


a_l=[chr(i) for i in range(ord("a"),ord("z")+1)]
if len(s)<26:print(s+sorted(set(a_l)-set(s))[0]);exit()


for i,x in enumerate(s[::-1]):
    l=[chr(i) for i in range(ord("a"),ord("z")+1) if ord(x)<i]
    for y in l:
        if Counter(s[:26-i-1]+y).most_common(1)[0][1]==1:
            print(s[:26-i-1]+y)
            exit()

print(-1)

Solution (Python):
def char2int(c):
    return ord(c) - ord('a')


def int2char(i):
    return chr(i + ord('a'))


def main():
    S = input()
    
    
    
    if len(S) < 26:
        used = [0] * 26
        for c in S:
            used[char2int(c)] = 1
        unused_min_c = int2char(min([i for i in range(26) if used[i] == 0]))
        print(S + unused_min_c)
    
    
    
    
    else:
        if S == 'zyxwvutsrqponmlkjihgfedcba':
            print(-1)
        else:
            used = [0] * 26
            for i in range(len(S) - 1, 1, -1):
                used[char2int(S[i])] = 1
                target_char = None
                for j in range(26):
                    if used[j] == 0:
                        continue
                    c = int2char(j)
                    if c > S[i - 1]:
                        if target_char is None:
                            target_char = c
                        else:
                            target_char = min(target_char, c)
                if target_char is None:
                    continue
                print(S[:(i - 1)] + target_char)
                return
            print(int2char(1 + char2int(S[0])))


if __name__ == '__main__':
    main()

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

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

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"

import string
s = v()

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

lis =list(string.ascii_lowercase)
nlis = [0]*26

for i in s:
    t = lis.index(i)
    nlis[t] += 1

if sum(nlis) != 26:
    for i in range(26):
        if nlis[i] == 0:
            print(s+lis[i])
            break
else:
    for i in range(25, -1, -1):
        for j in lis:
            if s[i] < j and j not in s[:i]:
                print(s[:i] + j)
                exit()

Solution (Python):
import string
s = input()

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

lis =list(string.ascii_lowercase)
nlis = [0]*26

for i in s:
    t = lis.index(i)
    nlis[t] += 1

if sum(nlis) != 26:
    for i in range(26):
        if nlis[i] == 0:
            print(s+lis[i])
            break
else:
    for i in range(25, -1, -1):
        for j in lis:
            if s[i] < j and j not in s[:i]:
                print(s[:i] + j)
                exit()

Solution (Python):
S = input()
a = 97
for i in range(26):
    if not chr(a + i) in S:
        S += chr(a + i)
        ans = S
        break
else:
    memo = [-1] * 200 
    flag = 0
    while True:
        i = 0
        while ord(S[-1]) + i < a + 26:
            
            if memo[ord(S[-1]) + i] == 1: 
                s = chr(ord(S[-1]) + i)
                S = S[:-1] + s
                ans = S
                flag = 1
                break
            i += 1
        if flag == 1:
            break
        memo[ord(S[-1])] = 1
        S = S[:-1]
        if len(S) <= 0:
            ans = -1
            break
print(ans)

Solution (Python):
s = input()
c = [0 for _ in range(26)]

for i in range(len(s)):
    c[ord(s[i])-97] = 1

if len(s) == 26:
    ind = len(s)-1

    for _ in range(len(s)):
        r = -1
        for i in range(ord(s[ind])-97+1, ord('z')-97+1):
            if c[i] == 0:
                r = i
                break

        if r != -1:
            print(s[:ind] + chr(r+97))
            exit()

        c[ord(s[ind])-97] = 0
        ind -= 1

    print(-1)

else:
    print(s + chr(c.index(0)+97))

Solution (Python):

import string


def main():
    S = input()
    S_temp = S
    ans_temp = 'zyxwvutsrqponmlkjihgfedcba'
    for i in range(len(S) + 1):
        for c in string.ascii_lowercase:
            if c not in S[:i]:
                S_temp = S[:i] + c
                if S < S_temp:
                    break
        if S < S_temp and S_temp < ans_temp:
            ans_temp = S_temp
    print(ans_temp if S < ans_temp else -1)


if __name__ == "__main__":
    main()

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

li = [False] * 26

for i in range(n):
    s1 = s[i]
    li[ord(s1) - ord('a')] = True

if n != 26:
    for i in range(26):
        if not li[i]:
            s += chr(ord('a') + i)
            print(s)
            exit()

else:
    if s == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        for i in range(2,27):
            s2 = s[-i:]
            if not all((s2[j] > s2[j+1] for j in range(i-1))):
                s3 = s2[0]
                s2 = list(s2)
                s2.sort()
                for j in range(i):
                    if s2[j] == s3:
                        print(s[:-i] + s2[j+1])
                        exit()

Solution (Python):
s = input()

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

if len(s) < 26:
    for i in "abcdefghijklmnopqrstuvwxyz":
        if i not in s:
            print(s+i)
            exit()

else:
    s = list(s)
    t = []
    for i in range(len(s)):
        t.append(s[-1-i])
        t.sort(reverse=True)
        if t != s[-1-i:]:
            l = s[-1-i]
            for j in range(ord(l)-ord("a")+1,26):
                if chr(j+ord("a")) in t:
                    l = [chr(j+ord("a"))]
                    break
            for j in range(i+1,len(s)):
                l.append(s[-1-j])
            print(*l[::-1],sep="")
            exit()
    print(chr(ord(s[0])+1))

Solution (Python):
S = input()
alp = 'abcdefghijklmnopqrstuvwxyz'
if len(S) < 26:
    for char in alp:
        if not char in S:
            print(S+char)
            exit()
else:
    for i in range(1,26)[::-1]:
        if S[i-1] < S[i]:
            for j in range(i,26)[::-1]:
                if S[j] > S[i-1]:
                    print(S[:i-1]+S[j])
                    exit()
    print(-1)

Solution (Python):
def next(x):
    return chr(ord(x)+1)

import sys

A=set([chr(ord("a")+k) for k in range(26)])
S=input()

if len(S)!=26:
    u=min(A-set(S))
    print(S+u)
    exit()

p=-1

for i in range(25):
    if S[i]<S[i+1]:
        p=i

if p==-1:
    print(-1)
else:
    T=S[:p]
    a=next(S[p])

    while a in T:
        a=next(a)
    print(T+a)

Solution (Python):
s = input()
l = "abcdefghijklmnopqrstuvwxyz"
flag = [False for i in range(26)]
for i in s:
    flag[l.index(i)] = True
if False in flag:
    print(s + l[flag.index(False)])
else:
    w = []
    for i in range(26):
        temp = s[-i - 1]
        w.append(temp)
        w.sort()
        if w[-1] == temp:
            continue
        else:
            print(s[:-i-1] + w[w.index(temp) + 1])
            break
    else:
        print(-1)

Solution (Python):

def is_various(s):
	if len(s) == len(set(s)):
		return True


import string
lower = string.ascii_lowercase

S = input()

for c in lower:
	next = S + c
	
	if  len(set(next)) == len(S) + 1:
		print(next)
		exit()

for i in range(1, len(S) + 1):
	left = S[:len(S)-i]
	right = S[-i:]
	
	start = right[0]
	is_start = False

	for c in lower:
		if is_start:
			next = left + c
			
			if is_various(next):
				print(next)
				exit()
		
		if c == start:
			is_start = True
		
print(-1)

Solution (Python):
import sys

S = sys.stdin.readline().strip()
ls = len(S)

if ls == 26 and list(S) == sorted(S, reverse=True):
    print(-1)
    sys.exit()

alpha = "abcdefghijklmnopqrstuvwxyz"
exists = set(S)
if ls < 26:
    for a in alpha:
        if a not in exists:
            print(S + a)
            sys.exit()
else:
    for i in range(ls):
        tmp = S[ls-1-i:] 
        if list(tmp) != sorted(tmp, reverse=True):
            break
    last = S[ls-i-1]
    for r in sorted(S[ls-i:], reverse=True):
        if last < r:
            min_a = r
    
    print(S[:ls-i-1] + min_a)

Solution (Python):
import sys

S = sys.stdin.readline().strip()
ls = len(S)

if ls == 26 and list(S) == sorted(S, reverse=True):
    print(-1)
    sys.exit()

alpha = "abcdefghijklmnopqrstuvwxyz"
exists = set(S)
if ls < 26:
    for a in alpha:
        if a not in exists:
            print(S + a)
            sys.exit()
else:
    for i in range(ls):
        tmp = S[ls-1-i:] 
        if list(tmp) != sorted(tmp, reverse=True):
            break
    remain = []
    exists = set(S[:ls-i-1])
    last = S[ls-i-1]
    for a in alpha:
        if a not in exists:
            remain.append(a)

    for r in sorted(remain, reverse=True):
        if last < r:
            min_a = r
    
    print(S[:ls-i-1] + min_a)

Solution (Python):
s = input()+chr(ord("a")-1)
ans = -1
for i in range(len(s)):
    t = s[:~i]
    dif = set(map(chr,range(ord(s[~i])+1,123))) - set(t)
    if dif:
        ans = t + min(dif)
        break
print(ans)

Solution (Python):
def solve():
    S = input()

    if len(S) < 26:
        for a in [chr(ord("a")+i) for i in range(26)]:
            if a in S:
                continue
            print(S+a)
            return


    for i in range(26):
        for a in [chr(j) for j in range(ord(S[-1-i]), ord("z")+1)]:
            if a in S[:len(S)-i]:
                continue
            print(S[:len(S)-i-1]+a)
            return

    else:
        print(-1)

solve()

Solution (Python):
s = input()
last = s[-1]
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
n = len(ascii_lowercase)

for moji in ascii_lowercase:
    if moji not in s:
        s += moji
        print(s)
        exit()

sl = list(s)
for i in range(len(s)-1, -1,-1):
    idx = ascii_lowercase.index(s[i])
    for j in range(idx,n):
        if ascii_lowercase[j] not in sl:
            sl[i] = ascii_lowercase[j]
            print(''.join(sl))
            exit()
    sl.pop()

if not sl:
    print(-1)

Solution (Python):
from collections import defaultdict
s = input()
n = len(s)
ans = -1

if len(s) < 26:
    alphabet_ls = [chr(i) for i in range(97, 97+26)]
    alphabet_dict = defaultdict(int)
    for i in range(n):
        alphabet_dict[s[i]] = 1
    for alphabet in alphabet_ls:
        if alphabet_dict[alphabet] == 0:
            ans = s + alphabet
            break

else:
    stock_ls = []
    for i in range(25,-1,-1):
        now = s[i]
        greater = []
        for stock in stock_ls:
            if now < stock:
                greater.append(stock)
        if greater:
            replacer = min(greater)
            ans = s[:i] + replacer
            break
        else:
            stock_ls.append(s[i])
print(ans)