Problem:


Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx



Sample input:
atcoder


Sample output:
atcoderb

Submission status: Accepted

Solution code:
s=input()
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(25)[::-1]:
            for j in range(i+1,26)[::-1]:
                if s[i]<s[j]:
                    print(s[:i]+s[j])
                    exit()

Submission status: Accepted

Solution code:
import string
S = input()
alph = string.ascii_lowercase

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

if set(S) == set(alph):
    L = []
    for i in range(len(S) - 1):
        T = S[:i]
        for a in alph:
            if not a in T and (T + a) > S[:i + 1]:
                L.append(T + a)
                break
    L.sort()
    print(L[0])
else:
    for a in alph:
        if not a in S:
            print(S + a)
            exit()

Submission status: Accepted

Solution code:
def main():
    S = input()
    from string import ascii_lowercase
    dic = {a: i for i, a in enumerate(ascii_lowercase)}
    rdic = {i: a for i, a in enumerate(ascii_lowercase)}
    if len(S) != 26:
        for s in ascii_lowercase:
            if s not in S:
                return print(S + s)
    elif S == "zyxwvutsrqponmlkjihgfedcba":
        return print(-1)

    pre = S[-1]
    mi = [pre]
    for i, s in enumerate(S[-2::-1], start=2):
        if s < pre:
            # 後ろから見て、'b' < 'd'のように初めて隣接する文字が昇順に
            # なる場所を探す。'd'以降の文字の中で、'b'より大きいもののうち最小
            # のものを'b'より前の文字列に足して出力
            # 'zyxwvutsrqponmlkjihgfebdca' -> 'zyxwvutsrqponmlkjihgfec'
            m = min(mi, key=lambda p: s > p)
            return print(S[:-i] + m)
        pre = s
        mi.append(s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
if len(s) < 26:
    for i in range(26):
        tmp = chr(i + ord('a'))
        if not tmp in s:
            print(s + tmp)
            exit()
else:
    for i in reversed(range(26)):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                print(s[:i] + chr(j))
                exit()
    print(-1)
    exit()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import copy
s = list(input())

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

if len(s) == 26:
    for i in reversed(range(0, 26)):
        #for j in range(1, ord('z') - ord(s[i])):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                s[i] = chr(j)
                print(''.join(s[:i + 1]))
                exit()

for i in range(26):
    if not chr(ord('a') + i) in s:
        s.append(chr(ord('a') + i))
        break
ans2 = ''.join(s)
print(ans2)

Submission status: Accepted

Solution code:
def main():
    S = input()
    from string import ascii_lowercase
    dic = {a: i for i, a in enumerate(ascii_lowercase)}
    rdic = {i: a for i, a in enumerate(ascii_lowercase)}
    if len(S) != 26:
        for s in ascii_lowercase:
            if s not in S:
                return print(S + s)
    elif S == "zyxwvutsrqponmlkjihgfedcba":
        return print(-1)

    pre = S[-1]
    mi = [pre]
    for i, s in enumerate(S[-2::-1], start=2):
        if s < pre:
            m = min(mi, key=lambda p: s > p)
            return print(S[:-i] + m)
        pre = s
        mi.append(s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
if len(s) < 26:
    for i in range(26):
        tmp = chr(i + ord('a'))
        if not tmp in s:
            print(s + tmp)
            exit()
if len(set(s)) == 26:
    for i in reversed(range(len(s))):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                print(s[:i] + chr(j))
                exit()
    print(-1)
    exit()

Submission status: Accepted

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

if len(S) < 26:
    for i in range(26):
        if not chr(ord("a") + i) in S:
            print(S + chr(ord("a") + i))
            exit()
else:
    pop_list = []
    S = list(S)
    while True:
        pop_chr = S.pop()
        pop_list.append(pop_chr)
        pop_list.sort()
        for candidate in pop_list:
            if S[-1] < candidate:
                S.pop()
                S.append(candidate)
                print(*S, sep="")
                exit()

Submission status: Accepted

Solution code:
sa = input()
s = list(sa)
a = [chr(ord('a') + i) for i in range(26)]
if sa == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    for i in range(len(a)):
        if a[i] not in s:
            s.append(a[i])
            print("".join(s))
            exit()
else:
    used = [s[25]]
    for i in range(25, 0, -1):
        if a.index(s[i-1]) > a.index(s[i]):
            used.append(s[i-1])
        else:
            for j in range(a.index(s[i-1])+1, 26):
                if a[j] in used:
                    s[i-1] = a[j]
                    print("".join(s[:i]))
                    exit()

Submission status: Accepted

Solution code:
s = input()
l = len(s)
backet = [0] * 26
if l != 26:
  for i in s:
    backet[ord(i) - 97] = 1
  for i in range(26):
    if not backet[i]:
      print(s + chr(i + 97))
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    d = []
    for i in s[::-1]:
      s = s[:-1]
      d.sort()
      for j in d:
        if j > ord(i)-97:
          print(s + chr(j+97))
          exit()
      d.append(ord(i)-97)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
from collections import Counter

s = input().rstrip()
n = len(s)
cnt = Counter(s)

for i in range(97, 97 + 26):
    key = chr(i)
    if key not in s:
        print(s + key)
        exit()

largest = s[-1]
seen = []
for i, ch in enumerate(s[::-1]):
    if ch < largest:
        for item in seen:
            if ch < item:
                s = s[:n-i-1] + item
                print(s)
                exit()
    largest = max(largest, ch)
    seen.append(ch)
    seen.sort()

print(-1)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
if len(s) < 26:
    for i in range(26):
        tmp = chr(i + ord('a'))
        if not tmp in s:
            print(s + tmp)
            exit()
if len(set(s)) == 26:
    for i in reversed(range(len(s))):
        for j in range(ord(s[i]) + 1, ord('z') + 1):
            if not chr(j) in s[:i]:
                print(s[:i] + chr(j))
                exit()
    print(-1)
    exit()
ans = [i for i in s]
for i in reversed(range(len(s))):
    tmp = chr(ans[i] + 1)
    if not tmp in ans:
        ans[i] = tmp
    print(''.join(ans))

Submission status: Accepted

Solution code:
from bisect import *

alpha = list(chr(c) for c in range(ord("a"), ord("z") + 1))

S = list(input())

not_used = list(set(alpha) - set(S))
not_used.sort()

if not not_used:
    can_use = []
    flag = False
    for i in range(len(S) - 1, -1, -1):
        # S[i]以上の値
        idx = bisect_left(can_use, S[i])
        if idx == len(can_use): # ない
            can_use.append(S[i])
        else: # ある
            S[i] = can_use[idx]
            idx = i
            flag = True
            break
    if flag:
        print(*S[:idx + 1], sep="")
    else:
        print(-1)
else:
    S.append(not_used[0])
    print(*S, sep="")

Submission status: Accepted

Solution code:
t="abcdefghijklmnopqrstuvwxyz"
s=input()
if s==t[::-1]:print(-1);exit()
if len(s)!=26:
  for i in t:
    if i not in s:print(s+i);exit()
i=25
while s[i-1]>s[i]:i-=1
tt=s[i-1]
ss=list(s[i-1:])
ss.sort()
print(s[:i-1]+ss[ss.index(tt)+1])

Submission status: Accepted

Solution code:
alp = list("abcdefghijklmnopqrstuvwxyz")
s=input()
n=len(s)
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif n<26:
    for i in alp:
        if i not in s:
            print(s+i)
            break
else:
    for i in range(len(s)-1,-1,-1):
        for j in range(ord(s[i])+1,ord('z')+1):
            if chr(j) not in s[:i]:
                print(s[:i]+chr(j))
                exit()

Submission status: Accepted

Solution code:
s = input()
alp = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) < 26:
    for i in alp:
        if i not in s:
            print(s+i)
            break
else:
    ans = ""
    for i in range(len(s)-1):
        f = True
        for j in range(i, len(s)-1):
            if ord(s[j]) < ord(s[j+1]):
                f = False
                break
        if f:
            t = sorted(list(s[i:]))
            for k in t:
                if ord(s[i-1]) < ord(k):
                    print(s[:i-1] + k)
                    break
            exit()
    print(s[:-2] + s[len(s)-1])

Submission status: Accepted

Solution code:
import collections
s  = list(input())
n = len(s)
alp = "abcdefghijklmnopqrstuvwxyz"
alp = list(alp)
co_alp = collections.Counter(alp)
if n==26:
    for i in range(n-1):
        if s[-i-1] >s[-i-2]:
            q = "z"
            for sj in s[-i-1:]:
                if sj >s[-i-2]:
                    q = min(q,sj)
            l = s[:-i-2]
            l.append(q)
            print("".join(l))
            break
    else:
        print(-1)
    exit()

for i in range(n):
    co_alp[s[i]]+=1
for w,num in sorted(co_alp.items()):
    if num==1:
        s.append(w)
        break
print("".join(s))

Submission status: Accepted

Solution code:
from string import ascii_lowercase


def main():
    word = input()
    if len(word) < 26:
        count = {}
        for a in ascii_lowercase:
            count[a] = 0
        for w in word:
            count[w] += 1
        count_l = list(count.items())
        count_l.sort(key=lambda x: x[0])
        add = ""
        for i in range(26):
            if count_l[i][1] == 0:
                add = count_l[i][0]
                break
        print(word + add)
    elif word == ascii_lowercase[::-1]:
        print(-1)
    else:
        is_printed = False
        while len(word) > 0 and not is_printed:
            for c in ascii_lowercase[ascii_lowercase.index(word[-1]) + 1:]:
                if c not in word[:-1]:
                    print(word[:-1] + c)
                    is_printed = True
                    break
            else:
                word = word[:-1]


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

def solve(S: str):
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        return

    if len(S) < 26:
        res = sorted(list(set("zyxwvutsrqponmlkjihgfedcba")-set(S)))
        print(S+res[0])
    else:
        prev = None
        for i in range(25,-1,-1):
            if prev == None or prev < S[i]:
                prev = S[i]
                continue
            else:
                res = sorted(list(set("zyxwvutsrqponmlkjihgfedcba")-set(S[:i+1])))
                for s in res:
                    if s > S[i]:
                        print(S[:i]+s)
                        return
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    solve(S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
alpha = "abcdefghijklmnopqrstuvwxyz"
invalpha = "zyxwvutsrqponmlkjihgfedcba"
D = {alpha[i]: i for i in range(26)}
E = [0 for i in range(26)]

for i in range(len(S)):
  E[D[S[i]]] = 1
if min(E) == 0:
  for m in range(26):
    if E[m] == 0:
      S = S[:] + alpha[m]
      break
  print(S)
elif S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
else:
  p = 26
  for i in range(25, 0, -1):
    if D[S[i]] < D[S[i-1]]:
      p = i
    else:
      break
  m = D[S[p-2]]
  L = []
  for x in range(p-1, 26):
    if D[S[x]] >= m + 1:
      L.append(S[x])
  L.sort()
  if L != []:
    S = S[:p-2] + L[0]
  else:
    S = S[:p-2] + 'z'
  print(S)

Submission status: Accepted

Solution code:
from bisect import bisect_left, insort_left

S = list(input())

alpha = [chr(i) for i in range(ord("a"), ord("a") + 26)]

not_used = list(set(alpha) - set(S))
not_used.sort()

if not not_used:
    can = []
    flag = True
    for i in range(len(S)-1, -1 , -1):
        idx = bisect_left(can, S[i])
        if idx == len(can):
            insort_left(can, S[i])
        else:
            S[i] = can[idx]
            idx = i
            flag = False
            break
    if flag:
        print(-1)
    else:
        print(*S[:idx+1], sep="")
else:
    S.append(not_used[0])
    print(*S, sep="")

Submission status: Accepted

Solution code:
s=input()
if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    quit()
elif len(s)<26:
    d=set([i for i in range(26)])
    for i in s:
        d.remove(ord(i)-97)
    print(s+chr(min(d)+97))
else:
    d=[ord(s[-1])]
    for i in range(25):
        c=s[-2-i]
        if max(d)<ord(c):
            d.append(ord(c))
        else:
            print(s[:26-2-i]+chr(min([i for i in d if i>ord(c)])))
            break

Submission status: Accepted

Solution code:
alpha = "abcdefghijklmnopqrstuvwxyz"

s = input()
if len(s) < 26:
  a = sorted(list(set(alpha) - set(s)))
  print(s+a[0])
  exit(0)

def a():
  A = [chr(i) for i in range(ord('a'), ord('z')+1)]
  B = {}
  for i in range(26):
    B[A[i]] = i
  C = [0]*26
  for i in range(len(s)):
    C[B[s[i]]] = 1
  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())

Submission status: Accepted

Solution code:
s=input()
if len(s)<26:
    for i in range(26):
        c=chr(ord("a")+i)
        if c not in s:
            print(s+c)
            exit()
else:
    for i in reversed(range(1,26)):
        if s[i-1]<s[i]:
            s1=s[0:i-1]
            for j in range(26):
                c=chr(ord("a")+j)
                if c > s[i-1] and c not in s1:
                    print(s1+c)
                    exit()
    print(-1)

Submission status: Accepted

Solution code:
s = input()

if len(s) < 26:
    # sに含まれていない文字の中で辞書順最小のものを付け加えれば良い
    for i in range(26):
        candi_ch = chr(ord('a') + i)
        if candi_ch not in s:
            print(s + candi_ch)
            exit()
else:
    for i in reversed(range(1, 26)):
        if s[i-1] < s[i]:
            s1 = s[0:i-1]
            for j in range(26):
                candi_ch = chr(ord('a') + j)
                if candi_ch > s[i-1] and candi_ch not in s1:
                    print(s1 + candi_ch)
                    exit()
    
    print(-1)

Submission status: Accepted

Solution code:
import sys
def main():
    s = input().rstrip()
    nums = [ord(c)-ord("a") for c in s]
    if len(s) < 26:
        c = ""
        for x in range(26):
            f = True
            for y in nums:
                if x == y:
                    f = False
                    break
            if f:
                c = chr(x + ord("a"))
                break
        print(s+c)
    else:
        if s == "zyxwvutsrqponmlkjihgfedcba":
            print("-1")
        else:
            x = -1
            for i in reversed(range(25)):
                if nums[i] < nums[i+1]:
                    x = i
                    break
            p = 26
            for i in range(x+1, 26):
                if nums[x] < nums[i] and nums[i] < p:
                    p = nums[i]
            print(s[:x]+chr(p+ord("a")))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = list(input())
q = set(list(s))
alpha = set([chr(i+ord('a')) for i in range(26)])
if alpha - q:
    use = alpha - q
    use = list(use)
    use.sort()
    print("".join(s) + use[0])
    exit()
for i in range(len(s)-1,-1,-1):
    asc = ord(s[i])
    q = set(s[:i+1])
    for j in range(asc,ord('z')+1):
        if chr(j) not in q:
            s[i] = chr(j)
            print("".join(s[:i+1]))
            exit()
print(-1)

Submission status: Accepted

Solution code:
S=input()
A=[chr(ord('a') + i) for i in range(26)]
if S=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
else:
  if len(S)<26:
    for i in A:
      if str(i) not in S:
        print(S+str(i))
        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()

Submission status: Accepted

Solution code:
S=input()
alphabet=[chr(i) for i in range(ord('a'),ord('z')+1)]
if len(S)!=26:
    for x in alphabet: 
        if not(x in set(S)):
            flag=1
            break
    ans=S+x
    print(ans)
else:
    s=ord(S[25])
    j=25
    for i in range(24,-1,-1):
        if s<ord(S[i]):
            j=i
            s=ord(S[i])
        else:
            break

    if j==0:
        print(-1)
    else:
        l=S[j:]
        y=300
        for x in list(map(ord,l)):
            if ord(S[j-1])<x:
                y=min(y,x)
        ans=S[:j-1]+chr(y)
        print(ans)

Submission status: Accepted

Solution code:
import string
s=input()
n=len(s)
l=string.ascii_lowercase
if n<26:
    for c in l:
        if c not in s:
            print(s+c)
            exit()
elif s==l[::-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()

Submission status: Accepted

Solution code:
S=input()
R="zyxwvutsrqponmlkjihgfedcba"
A=set(R)
if len(S)<=25:
    print(S+min(A-set(S)))
elif S==R:
    print(-1)
else:
    ok=set()
    while S:
        ok.add(S[-1])
        S=S[:-1]
        L=[s for s in ok if s > S[-1]]
        if L:
            print(S[:-1]+min(L))
            break

Submission status: Accepted

Solution 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))

Submission status: Accepted

Solution code:
def examA():
    S = SI()
    N = len(S)
    if N<26:
        d = defaultdict(bool)
        for s in S:
            d[s] = True
        for a in alphabet:
            if not d[a]:
                ans = S + a
                print(ans)
                return
    keep = []
    for i in range(N-1)[::-1]:
        keep.append(S[i+1])
        if S[i]<S[i+1]:
            break
    #print(keep)
    n = len(keep)
    c = "zz"
    for s in keep:
        if s>S[(N-n-1)]:
            c = min(c,s)
    if c=="zz":
        print(-1)
        return
    ans = S[:(N-n-1)] + c
    print(ans)
    return

def examB():
    N = I()
    ans = [2*(i+1) for i in range(N)]
    if N%2==0:
        for i in range(4998):
            ans[-3-i] = 3+i*6

    print(" ".join(map(str,ans)))
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examA()

"""

"""

Submission status: Accepted

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

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

if len(S) != 26:
    check =[[0] * 2 for _ in range(26)]
    for i in range(len(S)):
        for a in range(26):
            if S[i] == al[a]:
                check[a][0] = 1
                check[a][1] = i
    for i in range(26):
        if check[i][0] == 0:
            used_al = al[i]
            break
    print(S + used_al)
    exit()
ans = ""
lst = []
for i in range(24, -1, -1):
    if S[i] > S[i+1]:
        lst.append(S[i+1])
    else:
        lst.append(S[i+1])
        tmp = S[i]
        lst.append(S[i])
        ans += S[:i]
        lst.sort()
        for j in range(len(lst)):
            if tmp == lst[j]:
                ans += lst[j+1]
                break
        print(ans)
        exit()

Submission status: Accepted

Solution code:
s = input()

if len(s) < 26:
    a = [0 for _ in range(26)]

    for i in range(len(s)):
        a[ord(s[i]) - ord("a")] += 1

    ans = s
    for i in range(26):
        if a[i] == 0:
            ans += chr(i+ord("a"))
            break

    print(ans)
    exit()


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

print(-1)

Submission status: Accepted

Solution code:
s = input()

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

Submission status: Accepted

Solution code:
berb = ['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 = list(input())

for i in range(len(S)):
    S[i] = berb.index(S[i])

S_d = [-1]
S_0 = len(S)
k = 0

while True:
    if k == 26:
        if len(S) == 0:
            print(-1)
            break
        else:
            S_d.append(S.pop(-1))
            k = 0
    elif k in S or k <= S_d[-1]:
        k += 1
    else:
        S.append(k)
        for j in range(len(S)):
            S[j] = berb[S[j]]

        print(''.join(S))
        break

Submission status: Accepted

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

if len(S)<26:
    for s in S:
        abc.remove(s)
    print(S+abc[0])
    exit()
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()

print(-1)

Submission status: Accepted

Solution code:
def main():
    S = input()
    N = len(S)
    dic = ['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 d in dic:
        if d in S:
            continue
        else:
            return print(S + d)
    if S == "zyxwvutsrqponmlkjihgfedcba":
        return print(-1)
    pre = S[-1]
    used = [S[-1]]
    from heapq import heapify, heappop, heappush
    heapify(used)
    for i in range(N-1)[::-1]:
        if S[i] < pre:
            c = heappop(used)
            while S[i] > c:
                c = heappop(used)
            print(S[:i] + c)
            break
        else:
            pre = S[i]
            heappush(used, pre)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# AGC022 A - Diverse Word
s=input()
n=len(s)
mozi=[0]*26
ans=''
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif n<26:
    for ss in s:
        mozi[ord(ss)-97]+=1
    for i in range(len(mozi)):
        if mozi[i]==0:
            break
    ans=s
    ans+=chr(97+i)
    print(ans)
elif n==26:
    for i in range(1,27):
        if s[-i]>s[-i-1]:
            break
    ss=sorted(s[-i:])
    for sss in ss:
        if sss>s[-i-1]:
            break
    print(s[:-i-1]+sss)

Submission status: Accepted

Solution code:
alp = 'abcdefghijklmnopqrstuvwxyz'
s = input()
if s == alp[::-1]:
    print(-1)
elif len(s) == 26:
    for i in range(len(s)-1,0,-1):
        if s[i] > s[i-1]:
            for j in alp:
                if j not in s[:i] and j > s[i-1]:
                    print(s[:i-1]+j)
                    break
            break
else:
    for i in alp:
        if i not in s:
            print(s+i)
            break

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=list(ST())
    if S==list("abcdefghijklmnopqrstuvwxyz"):
        print("abcdefghijklmnopqrstuvwxz")
        return 
    exist = defaultdict(int)
    N=len(S)
    for s in S:
        exist[s]+=1
    if len(S)==26:
        flag = False
        for i in range(N)[::-1]:
            s = chr(ord("a")+i)
            if S[i]==s:
                flag = True
                continue
            if flag:
                exist_s = set(S[:i+1])
                for j in range(26):
                    t = chr(ord("a")+j)
                    if t not in exist_s and t>S[i]:
                        S[i]=t
                        break
            else:
                done = False
                for i in range(N-1)[::-1]:
                    if S[i]<S[i+1]:
                        for j in range(N)[::-1]:
                            if S[i]<S[j]:
                                S[i],S[j] = S[j],S[i]
                                # S[i+1:] = S[i+1:][::-1]
                                S=S[:i+1]
                                done = True
                                break
                    if done:
                        break
                if done:
                    print(*S,sep="")
                else:
                    print(-1)
                return
            break
        print(*S,sep="")
    else:
        for i in range(26):
            s = chr(i+ord("a"))
            if exist[s]==0:
                S+=s
                break
        print(*S,sep="")
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S=input()
if S=='zyxwvutsrqponmlkjihgfedcba':
  print(-1)
  exit()
alp='abcdefghijklmnopqrstuvwxyz'
N=len(S)
if N<26:
  T=S+alp
  i=0
  while T.index(alp[i])<N:
    i+=1
  print(S+alp[i])
  exit()
  
i=-1
nums=[alp.index(S[i])]
while max(nums)==nums[-1]:
  i-=1
  nums.append(alp.index(S[i]))

out=S[:-len(nums)]
tmp=26
for i in nums[:-1]:
  if i>nums[-1] and tmp>i:
    tmp=i
    
print(out+alp[tmp])