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 math
import bisect
import collections
import itertools
import heapq
import string
import sys
sys.setrecursionlimit(10**9)
def gcd(a,b):return math.gcd 
def lcm(a,b):return (a*b) // math.gcd(a,b) 
def iin(): return int(input()) 
def isn(): return input().split() 
def imn(): return map(int, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def comb(l, n): return itertools.combinations(l, n) 
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i: divisors.append(n//i)
    return divisors
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n + 1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def sieve_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime
s = input()
S = list(s)
a = string.ascii_lowercase
od = collections.OrderedDict()
for i in range(len(a)):
    od[a[i]] = 0
for st in S:
    od[st] = 1
for k, v in od.items():
    if v == 0:
        print(join(S)+k)
        exit()

prev = S.pop()
od[prev] = 0
done = False
for i in range(len(S)):
    prev = S.pop()
    od[prev] = 0
    target = False 
    for k,v in od.items():
        if prev == k:
            target = True
            continue
        if target == True and v == 0:
            print(join(S)+k)
            done = True
            break
    if done == True:
        break
if done == False:
    print(-1)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    s = S()
    if "".join(s) == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return
    l = [chr(ord("a") + i) for i in range(26)]
    for si in s:
        l.remove(si)
    if l == []:
        s = s[::-1]
        l.append(s[0])
        ans = []
        f = True
        for si in s[1:]:
            if f:
                if l[-1] > si:
                    f = False
                    x = si
                else:
                    l.append(si)
            else:
                ans.append(si)
        l.sort()
        for ai in l:
            if ai > x:
                print("".join(ans[::-1]) + ai)
                return
    else:
        print("".join(s) + l[0])
    return



if __name__ == '__main__':
    solve()

Solution (Python):
import sys
from string import ascii_lowercase as alp
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	S = input().rstrip()

	
	for i, letter in enumerate(alp):
		if not S.count(letter):
			print(S+letter)
			sys.exit()

	
	
	
	
	tmp = []
	for i in range(len(S)-1, 0, -1):
		if S[i] < S[i-1]:
			tmp.append(S[i])
		elif S[i] > S[i-1]:
			tmp.append(S[i])
			tmp.sort()
			
			for t in tmp:
				if S[i-1] < t:
					print(S[:i-1]+t)
					sys.exit()
	
	
	print(-1)

if __name__ == '__main__':
	main()

Solution (Python):
import sys
from string import ascii_lowercase as alp
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	S = input().rstrip()
	
	if S == alp[::-1]:
		print(-1)
		sys.exit()
	
	for i, letter in enumerate(alp):
		if not S.count(letter):
			print(S+letter)
			sys.exit()
	
	
	tmp = []
	for i in range(len(S)-1, 0, -1):
		if S[i] < S[i-1]:
			tmp.append(S[i])
		elif S[i] > S[i-1]:
			tmp.append(S[i])
			tmp.sort()
			
			for t in tmp:
				if S[i-1]<t:
					print(S[:i-1]+t)
					sys.exit()

if __name__ == '__main__':
	main()

Solution (Python):
S = input().strip()
C = {chr(i):0 for i in range(97,123)}
n = len(S)
for a in S:
    C[a] += 1
flag = 0
for i in range(97,123):
    if C[chr(i)]==0:
        x = S+chr(i)
        flag = 1
        break
if flag==0:
    cnt = -2
    while cnt>=-n:
        b = S[cnt]
        A = []
        for i in range(n+cnt+1,n):
            if ord(b)<ord(S[i]):
                A.append(S[i])
        if len(A)>0:
            A = sorted(A)
            x = S[:cnt]+A[0]
            flag = 1
            break
        cnt -= 1
if flag == 1:
    print(x)
else:
    print(-1)

Solution (Python):
from string import ascii_lowercase
from sys import stdin, exit
S = stdin.readline().rstrip()
set_s = set(S)

if len(S) < 26:
  for c in ascii_lowercase:
    ans = S+c
    if set_s != set(ans) and S < ans:
      print(ans)
      exit()
else:
  for i in range(len(S)-1, -1, -1):
    for c in ascii_lowercase:
      if S[i] < c and c not in S[:i]:
        print(S[:i]+c)
        exit()
print(-1)

Solution (Python):
s = input()
included = set()
for i in s:
    included.add(i)
for i in range(ord('a'), ord('z') + 1):
    if not chr(i) in included:
        print(s + chr(i))
        exit()
ans = ''
for i in range(len(s) - 1, -1, -1):
    for j in range(ord(s[i]), ord('z') + 1):
        now = s[:i] + chr(j)
        if not chr(j) in s[:i] and s[:i + 1] != now:
            print(now)
            exit()
print(-1)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = read().rstrip().decode('utf-8')

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

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

def F_long(S):
    if S == ''.join(sorted(alphabet, 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)
answer = F_short(S) if L < 26 else F_long(S)
print(answer)

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

Solution (Python):
import string

S = input()
if len(S) < 26:
  for a in string.ascii_lowercase:
    if a not in S:
      print(S+a)
      exit()
else:
  S = list(S)
  L = [S[len(S)-1]]
  for i in range(24,-1,-1):
    for a in sorted(L):
      if a > S[i]:
        if i > 0:
          print(''.join(S[:i])+a)
        else:
          print(a)
        exit()
    L.append(S[i])
  print(-1)

Solution (Python):
S = input()
N = len(S)
alp = 'abcdefghijklmnopqrstuvwxyz'
if N<25:
  for a in alp:
    if a not in S:
      print(S+a)
      exit()
else:
  for i in range(N-1,-1,-1):
    nextS = ''
    for s in S[i:]:
      tmpS = S[:i] + s
      if tmpS == S:continue
      
      if nextS == '':
        if sorted([S,tmpS])[1] == tmpS:
          nextS = tmpS
      elif sorted([S,tmpS])[1] == tmpS and sorted([tmpS,nextS])[0] == tmpS:
        nextS = tmpS
     
    if nextS != '':
      print(nextS)
      exit()
  print(-1)

Solution (Python):

S = input()

can_use = [True for n in range(ord('a'), ord('z') + 1)]
for s in S:
    i = ord(s) - ord('a')
    can_use[i] = False

if any(can_use):
    for n, use in enumerate(can_use):
        if use:
            S += chr(ord('a') + n)
            print(S)
            exit()
else:
    
    for i in range(len(S)-1, 0, -1):
        if ord(S[i-1]) < ord(S[i]):
            c = sorted([c for c in S[i-1:] if c > S[i-1]])[0]
            ans = S[:i-1] + c
            print(ans)
            exit()

    print(-1)

Solution (Python):
s = input()
len_s = len(s)
x = "abcdefghijklmnopqrstuvwxyz"
ls = {i : j for j, i in enumerate(x)}
if len_s < 26:
    set_s = set(s)
    candidate = list(set(x).difference(set_s))
    candidate.sort()
    print(s + candidate[0])
else:
    if s == x[::-1]:
        print(-1)
    else:
        res = ""
        inv_s = s[::-1]
        _max = -1
        for i, j in enumerate(inv_s):
            if _max > ls[j]:
                break
            _max = max(_max, ls[j])
        res = s[:-1-i]
        candidate = [k for k in inv_s[:i] if k > j]
        print(res + candidate[0])

Solution (Python):
S = input()

if len(S) == 26:
    
    pointa = -1
    for i in reversed(range(1, 26)):
        if ord(S[i]) > ord(S[i-1]):  
            pointa = i-1
            break
    
    if pointa != -1:
        happend = list(S[:pointa])  
        for i in range(26):
            if chr(ord("a") + i) not in happend and ord("a")+i > ord(S[pointa]):
                target = chr(ord("a") + i)
                break
        print(S[:pointa]+target)
    else:
        print(-1)

else:
    
    happend = list(S)
    for i in range(26):
        if chr(ord("a") + i) not in happend:
            target = chr(ord("a") + i)
            break
    print(S+target)

Solution (Python):
import sys

S = list(input())
    
for i in range(len(S)):
    S[i] = ord(S[i]) - ord("a")

if len(S) == 26:
    t = 26
    for i in range(len(S)-1)[::-1]:
        for j in range(i+1,26):
            if S[i] < S[j] and t > S[j]:
                t = S[j]
        if t != 26:
            S = S[:i] + [t]
            for k in range(len(S)):
                S[k] = chr(S[k] + ord("a"))
            print("".join(S))
            sys.exit()
    print(-1)

else:
    n = set(range(26)) - set(S)
    S.append(min(n))
    for i in range(len(S)):
        S[i] = chr(S[i] + ord("a"))
    print("".join(S))

Solution (Python):
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if len(S) == 26:
    now = S[-1]
    for i in reversed(range(26)):
        if ord(now) > ord(S[i]):  
            happened = list(S[:i+1])
            for j in range(26):
                if chr(ord("a") + j) not in happened and ord("a") + j > ord(S[i]):
                    print(S[:i]+chr(ord("a") + j))
                    exit()
        else:
            now = S[i]

else:
    happened = list(S)
    for i in range(26):
        if chr(ord("a") + i) not in happened:
            print(S+chr(ord("a") + i))
            exit()

Solution (Python):
import sys
import bisect

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  

a2z = set(list(map(chr, range(97, 123))))
S = rs()
if len(S) < 26:
    unused = a2z - set(S)
    print(S + sorted(list(unused))[0])
    exit()
else:
    l = [S[25]]
    for i in range(24, -1, -1):
        j = bisect.bisect_left(l, S[i])
        if j != len(l):
            answer = S[:i] + l[j]
            print(answer)
            exit()
        else:
            l.append(S[i])
            l.sort()
print(-1)

Solution (Python):
s = list(input())

n = len(s)
a = "abcdefghijklmnopqrstuvwxyz"
ans = -1
if n < 26:
    for v in a:
        if v not in s:
            ans = "".join(s + [v])
            break
else:
    for i in range(1, n)[::-1]:
        if s[i-1] < s[i]:
            j = a.index(s[i-1])
            for v in a[j:]:
                if v not in s[:i]:
                    s[i-1] =  v
                    ans = "".join(s[:i])
                    break
            break

print(ans)

Solution (Python):
from sys import stdin
import string

s = stdin.readline().rstrip()
l = []
for i in string.ascii_lowercase:
    l.append(i)

for i in s:
    l.remove(i)

if s == string.ascii_lowercase[::-1]:
    print(-1)
    exit()
elif len(l) > 0:
    print(s+l[0])
else:
    k = s[::-1]
    li = [k[0]]
    for i in range(1,len(k)):
        li.append(k[i])
        if string.ascii_lowercase.index(k[i]) < string.ascii_lowercase.index(k[i-1]):
            a = li[-1]
            li.sort()
            print(s[:len(s)-i-1]+li[li.index(a)+1])
            exit()

Solution (Python):
import string
A = [chr(i) for i in range(ord('a'), ord('z')+1)]
B = {}
for i in range(26):
    B[A[i]] = i
s = input()
n = len(s)
def a():
    C = [0] * 26
    for i in range(n):
        C[B[s[i]]] = 1

    if n < 26:
        i = 0
        while C[i] == 1:
            i += 1
        return s + A[i]
    else:
        for i in range(25, -1, -1):
            t = s[i]
            for j in range(B[s[i]]+1, 26):
                if C[j] == 0:
                    return s[:i] + A[j]
            C[B[t]] = 0
        return -1
print(a())

Solution (Python):
S = input()
L = [i for i in range(97,123)]
n = len(S)
for i in range(n):
    L.remove(ord(S[i]))
if n <= 25:
    X = L[0]
    ans = S + chr(X)
else:
    T = []
    x = S[-1]
    T.append(x)
    for i in range(2,27):
        y = S[-i]
        if y > x:
            x = y
            T.append(y)
        else:
            break
    m = 26 - len(T)
    if m > 1:
        ans = S[0]
        for i in range(1,m-1):
            ans += S[i]
        for t in T:
            if t > S[m-1]:
                ans += t
                break
    elif m == 0:
        ans = -1
    else:
        for t in T:
            if t > S[m-1]:
                ans = t
                break
print(ans)

Solution (Python):
s = input()
a = [chr(i) for i in range(97, 97 + 26)]

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

if len(s) != 26:
    print(s + sorted(list(set(a) - set(s)))[0])
else:
    t = s
    s = list(s)
    n = len(s)
    for i in range(n - 1)[::-1]:
        if s[i] < s[i + 1]:
            break
    for j in range(n)[::-1]:
        if s[i] < s[j]:
            s[i], s[j] = s[j], s[i]
            break
    s = s[:i + 1] + s[i + 1:][::-1]

    ans = ""
    for i, j in zip(s, t):
        ans += i
        if i != j:
            print(ans)
            exit()

Solution (Python):
s = input()
a = [chr(i) for i in range(97, 97 + 26)]

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

if len(s) != 26:
    print(s + sorted(list(set(a) - set(s)))[0])
else:
    t = s
    s = list(s)
    n = len(s)
    for i in range(n - 1):
        if s[i] < s[i + 1]:
            j = i
    for i, v in enumerate(s[::-1]):
        if s[j] < v:
            s[j], s[n-i-1] = s[n-i-1], s[j]
            break
    s = s[:j + 1] + s[j + 1:][::-1]

    ans = ""
    for i, j in zip(s, t):
        ans += i
        if i != j:
            print(ans)
            exit()

Solution (Python):
s = input()
a = [chr(i) for i in range(97, 97 + 26)]

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

if len(s) != 26:
    print(s + sorted(list(set(a) - set(s)))[0])
else:
    j = 0
    for i in range(len(s) - 1):
        if s[i] < s[i + 1]:
            j = i
    x = []
    for i in s[j + 1:]:
        if s[j] < i:
            x.append(i)
    print(s[:j] + min(x))

Solution (Python):
from string import ascii_lowercase
D = {k:i for i, k in enumerate(ascii_lowercase)}

S = input()
n = len(S)

if n != 26:
    ans = S
    for c in ascii_lowercase:
        if c not in S:
            ans += c
            break
else:
    nokori = [D[S[-1]]]
    for i in range(24, -1, -1):
        res = 30
        for n in nokori:
            if D[S[i]] < n:
                res = min(res, n)
        if res == 30:
            nokori.append(D[S[i]])
        else:
            ans = S[:i] + ascii_lowercase[res]
            break
    else:
        ans = -1

print(ans)

Solution (Python):
S = [ord(i) for i in input()] + [96]

def solve() :
  for i in range(len(S) - 1, -1, -1) :
    for j in range(S[i] + 1, 97 + 26) :
      if not j in S[:i+1] :
        S[i] = j
        return ''.join(map(chr, S[:i+1]))
  return -1

print(solve())

Solution (Python):
S = list(input())
N = len(S)
al=[chr(ord('a') + i) for i in range(26)]


if N < 26:
  for i in range(26):
    if al[i] not in S:
      S.append(al[i])
      print(*S,sep="")
      exit()
else:
  flg = False
  S = S[::-1]
  x = 0
  while x < len(S):
    id = al.index(S[x])
    if id == 25:
      S = S[x+1:]
    else:
      id += 1
      for k in range(id,26):
        if al[k] not in S:
          S[x] = al[k]
          S = S[::-1]
          print(*S,sep="")
          exit()
      S = S[x+1:]
  print(-1)

Solution (Python):
s=input()
ans=""

slist=list(s)
if(len(slist)==26):
    if(s=='zyxwvutsrqponmlkjihgfedcba'):
        print(-1)
    else:
        tlist=list(s)
        while(''.join(tlist)!='zyxwvutsrqponmlkjihgfedcba'):
            ttail=tlist.pop(-1)
            while(ttail=='z'):
                ttail=tlist.pop(-1)
            tlist.append(chr(ord(ttail)+1))

            if(len(tlist) == len(list(set(tlist)))):
                break

        print(''.join(tlist))
else:
    tlist=list(s)
    tlist.append('a')
    c=97
    while(ord('z') >= c):
        tlist[-1]=chr(c)
        if(len(list(set(slist))) != len(list(set(tlist)))):
            break
        else:
            c+=1
    print(''.join(tlist))

Solution (Python):
S=input()
for i in range(ord('a'),ord('z')+1):
  if chr(i) not in S:
    print(S+chr(i))
    break
else:
  for i in range(len(S)-1,-1,-1):
    for j in range(ord('a'),ord('z')+1):
      if j>ord(S[i]) and chr(j) not in S[:i]:
        print(S[:i]+chr(j))
        exit(0)
  else:
    print(-1)

Solution (Python):
S = input()

for i in range(ord('a'), ord('z')+1):
    if chr(i) not in S:
        print(S + chr(i))
        exit()
else:
    for i in range(len(S)-1, -1, -1):
        for j in range(ord('a'), ord('z')+1):
            if S[i] < chr(j) and chr(j) not in S[:i]:
                print(S[:i] + chr(j))
                exit()
    else:
        print(-1)

Solution (Python):
s = input()
alp = "abcdefghijklmnopqrstuvwxyz"

if len(s) < 26:
    for i in alp:
        if not i in s:
            ans = s + i
            break

else:
    tmp = []
    ss = list(s)
    for i in range(len(ss)):
        if i == 0:
            tmp.append(ss.pop())
            continue

        if alp.index(ss[-1]) > alp.index(tmp[-1]):
            tmp.append(ss.pop())
            continue
        else:
            remove = ss.pop()
            tmp.sort()
            for j in tmp:
                if j > remove:
                    renew = j
                    break
            ans = "".join(ss) + renew
            break
    else:
        ans = -1

print(ans)

Solution (Python):
S=input()
for i in range(ord('a'),ord('z')+1):
  if chr(i) not in S:
    print(S+chr(i))
    break
else:
  for i in range(len(S)-1,-1,-1):
    for j in range(ord('a'),ord('z')+1):
      if j>ord(S[i]) and chr(j) not in S[:i]:
        print(S[:i]+chr(j))
        exit(0)
  else:
    print(-1)

Solution (Python):
s = input()
n = len(s)
alph = "abcdefghijklmnopqrstuvwxyz"
to_int = {}
for i in range(n):
    to_int[alph[i]] = i

set_ = set([])
for i in range(n):
    set_.add(s[i])

for char in alph:
    if char not in set_:
        print(s + char)
        exit()

for i in range(n)[::-1]:
    ii = to_int[s[i]]
    for j in range(ii+1, 26):
        if alph[j] not in set_:
            print(s[0:i] + alph[j])
            exit()
    set_.remove(s[i])
print(-1)

Solution (Python):
s=input()
n=len(s)
if n<26:
    check=[False]*26
    for i in range(26):
        if chr(97+i) in s: check[i]=True
    print(s+chr(97+check.index(False)))
if n==26:
    if s[::-1]=='abcdefghijklmnopqrstuvwxyz':
        print(-1)
    else:
        for i in range(n)[::-1]:
            f=False
            l=[]
            for j in range(i,n):
                if s[i]<s[j]:
                    l.append(s[j])
            if l==[]:
                continue
            else:
                l.sort()
                print(s[:i]+l[0])
                f=True
            if f:break

Solution (Python):

import sys
from string import ascii_lowercase as alphabet
from bisect import bisect_left as bi_l

def main():
    s = sys.stdin.readline().rstrip()
    n = len(s)
    used = set(s)
    not_used = sorted(set(alphabet) - used)

    if not_used:
        ans = s +  not_used[0]
    else:
        cand = []
        for i in range(n-1, 0, -1):
            cand.append(s[i])
            if s[i-1] > s[i]:
                continue
            else:
                ans = s[:i-1] + cand[bi_l(cand, s[i-1])]
                break
        else:
            ans = -1
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
from string import ascii_lowercase as alphabet
from bisect import bisect_left as bi_l

index = dict((a, i) for i, a in enumerate(alphabet))

def main():
    s = sys.stdin.readline().rstrip()
    n = len(s)
    used = set(s)
    not_used = sorted(set(alphabet) - used)

    if not_used:
        ans = s +  not_used[0]
    else:
        cand = []
        for i in range(n-1, 0, -1):
            cand.append(s[i])
            if s[i-1] > s[i]:
                continue
            else:
                ans = s[:i-1] + cand[bi_l(cand, s[i-1])]
                break
        else:
            ans = -1
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
A = 'abcdefghijklmnopqrstuvwxyz'
if len(S) <= 25:
    for i in range(26):
        if A[i] not in S:
            print(S+A[i])
            break
else:
    for i in range(25):
        if S[-1-i] > S[-2-i]:
            for j in range(i+1):
                if S[-2-i] < S[-1-j]:
                    print(S[:-2-i]+S[-1-j])
                    break
            break
    else:
        print(-1)

Solution (Python):
import string
S = input()
ans = ''
if len(S) != 26:
    for i in string.ascii_lowercase:
        if S.count(i) == 0:
            ans = S + i
            break
else:
    tmp = []
    tmp.append(S[-1])
    loopF = False
    for i in range(len(S) - 2, -1, -1):
        for j in range(len(tmp)):
            if S[i] < tmp[j]:
                ans = S[0:i] + tmp[j]
                loopF = True
                break

            if j == len(tmp) - 1:
                tmp.append(S[i])
                tmp.sort()
        if loopF:
            break
if ans == '':
    print(-1)
else:
    print(ans)

Solution (Python):
S = [ord(i)-97 for i in str(input())]
appeared = [False for i in range(26)]

if len(S)<26:
    alp="".join([chr(i) for i in range(97,97+26)])
    s = "".join([chr(i+97) for i in S])
    for i in alp:
        if i not in s:
            print(s+i)
            exit()
    """
    for s in S:
    appeared[s] = True
    for i in range(len(appeared)):
        if appeared[i] == False:
            break
    S.append(i)
    print("".join([chr(i+97) for i in S]))
    exit()
    """
    
else:
    s = "".join([chr(i+97) for i in S])
    if s=="".join([chr(i) for i in range(97,97+26)][::-1]):
        print(-1)
        exit()
    
    prev = -1
    for i in [25-k for k in range(26)]:
        if S[i] < prev:
            break
        prev = S[i]
    for i in range(24,-1,-1):
        for j in range(25,i,-1):
            if S[i]<S[j]:
                S = S[:i]+[S[j]]
                print("".join([chr(i+97) for i in S]))
                exit()
    """
    string = S[i+1:]
    for j in range(len(string)):
        if string[j] < S[i]:
            string[j] = 100
    S = S[:i]+[min(string)]
    """

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



S = sys.stdin.buffer.readline().decode().rstrip()
if string.ascii_lowercase[::-1].startswith(S):
    print(-1)
    exit()

for i in reversed(range(len(S))):
    
    for c in list(string.ascii_lowercase):
        if c not in S:
            print(S + c)
            exit()
    for j in range(ord(S[i]) + 1, ord('z') + 1):
        if chr(j) not in S[:i]:
            print(S[:i] + chr(j))
            exit()

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

Solution (Python):
abc="abcdefghijklmnopqrstuvwxyz"
import collections
table=collections.defaultdict(int)
for i in range(26):
  table[abc[i]]=i
table2=collections.defaultdict(int)
s=input()
for i in reversed(range(len(s)+1)):
  k=0
  if i < len(s):
    k=table[s[i]]+1
  for j in range(k,26):
    if abc[j] in s[:i]:
      continue
    else:
      print(s[:i]+abc[j])
      exit(0)
print(-1)

Solution (Python):
s = input()
a = 'abcdefghijklmnopqrstuvwxyz'

if len(s) < 26:
    for w in a:
        if not w in s:
            print(s + w)
            exit()

if len(s) == 26:
    for i in range(len(s)-2, -1, -1):
        for j in range(len(s)-1, i, -1):
            if s[i] < s[j]:
                print(s[:i]+s[j])
                exit()

print('-1')

Solution (Python):
def main():
    s = input()

    if len(s) < 26:
        for i in range(ord("a"), ord("z") + 1):
            c = chr(i)
            if c not in s:
                ans = s + c
                break

    else:
        ans = -1
        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]:
                        ans = s[:i-1] + s[j]
                        break
                break

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import string

S = input()
all_s_list = list(string.ascii_lowercase)
remain_s_list = [x for x in all_s_list if x not in S]

s_dict = {c: i + 1 for i, c in enumerate(all_s_list)}

if remain_s_list:
    ans = S + remain_s_list[0]
else:
    last_s = "".join(reversed(all_s_list))
    if S == last_s:
        ans = -1
    else:
        tail_list = []
        for i in range(1, len(all_s_list) + 1):
            char = S[-i]
            w_list = [x for x in tail_list if s_dict[char] < s_dict[x]]
            w_list = sorted(w_list)
            if w_list:
                if i != len(all_s_list):
                    ans = S[:-i] + w_list[0]
                else:
                    ans = w_list[0]
                break
            tail_list.append(char)

print(ans)

Solution (Python):
def main():
    s=list(input())
    alpha=[chr(i) for i in range(97, 97+26)]
    if s==list("zyxwvutsrqponmlkjihgfedcba"):
        print(-1)
        return
    if len(s)!=26:
        for i in range(26):
            a=alpha[i]
            if a not in s:
                s.append(a)
                print(''.join(s))
                return
    else:

        tank=[s[25]]
        for i in range(25):
            if ord(s[24-i])>ord(s[25-i]):
                tank.append(s[24-i])
            else:
                point=s[24-i]
                tank.append(s[24-i])
                tank.sort()
                for j in range(len(tank)):
                    if tank[j]==point:
                        head=tank.pop(j+1)
                        break
                s=s[:24-i]
                s.append(head)
                print(''.join(s))
                return                




if __name__ == "__main__":
    main()

Solution (Python):
alphabet=[chr(i+97) for i in range(0,26)]
S=input()
if S=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for i in S:
        alphabet.remove(i)
    if alphabet:
        print(S+alphabet[0])
    else:
        motoS=S
        S=list(S)
        poplist=[]
        stringlist=[]
        while len(S)>=2:
            poplist.append(S[-1])
            S.pop()
            for s in poplist:
                _S=[i for i in S]
                _S[-1]=s
                string=""
                for ss in _S:
                    string+=ss
                stringlist.append(string)
        stringlist.append(motoS)
        stringlist.sort()
        try:print(stringlist[stringlist.index(motoS)+1])
        except:print(stringlist[stringlist.index(motoS)])

Solution (Python):
S=input()
if len(S)!=26:
    l=[0]*26
    for i in S:
        l[ord(i)-97]=1
    print(S,end="")
    for i in range(26):
        if l[i]==0:
            print(chr(i+97))
            break
else:
    l=[]
    for i in S:
        l.append(i)
    t=l[-1]
    for i in range(25,-1,-1):
        if t>l[i]:
            print(S[:i],end="")
            for j in S[::-1]:
                if j>l[i]:
                    print(j)
                    break
            break
        else:
            t=l[i]
    else:
        print(-1)