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: Wrong Answer

Solution code:
s=input()

ans=""

if len(s)<26:
    cnt_alphabet=[0]*26

    for i in range(len(s)):
        cnt_alphabet[ord(s[i])-97]+=1
    
    moji=""
    for i in range(26):
        if cnt_alphabet[i]<1:
            moji=chr(i+97)
            break
    ans=s+moji

else:
    cnt=25
    
    for i in range(24,-1,-1):
        if ord(s[i])>ord(s[i+1]):
            cnt=i
        else:
            break
    
    if cnt==1:
        ans='-1'
    else:
        min_chr=ord('{')
        for m in s[cnt:]:
            if ord(m)>ord(s[cnt-1]):
                min_chr=min(min_chr,ord(m))
            
        ans=s[:cnt-1]+chr(min_chr)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
d = defaultdict(int)
a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

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

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

Submission status: Wrong Answer

Solution code:
# 8
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


s = S()
alps = [chr(i) for i in range(ord('a'), ord('z') + 1)]
ans = -1
set_s = set(list(s))
# 辞書順で s 以降の単語を探す
for c in alps:
    if c not in set_s:
        ans = s + c
        break
print(ans)


# l = ['atcoder', 'atcodeo', 'atcoderb']
# l2 = ['a', 'aaa', 'ab']
# print(sorted(l))
# print(sorted(l2))

Submission status: Wrong Answer

Solution code:
S = input()
if len(S) != 26:
  A = [0] * 26
  offset = ord('a')
  for s in S:
    A[ord(s)-offset] = 1
  print(S + chr(A.index(0)+offset))
elif S == 'zyxwvutsrqponmlkjihgfedcba':
  print(-1)
else:
  T = 'z'*26
  for i in range(1, 25):
    T = min(T, S[:i] + min(S[i+1:]))
  print(T)

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase
S = input()
if len(S) != 26:
  A = [0] * 26
  offset = ord('a')
  for s in S:
    A[ord(s)-offset] = 1
  print(S + chr(A.index(0)+offset))
elif S == 'zyxwvutsrqponmlkjihgfedcba':
  print(-1)
else:
  print(S[:24] + S[-1])

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase
S = input()
if len(S) != 26:
  A = [0] * 26
  offset = ord('a')
  for s in S:
    A[ord(s)-offset] = 1
  print(S + chr(A.index(0)+offset))
elif S == 'zyxwvutsrqponmlkjihgfedcba':
  print(-1)
else:
  for x, y in zip(ascii_lowercase, S):
    if x == y:
      print(x, end='')
    else:
      print(x)
      break

Submission status: Wrong Answer

Solution code:
S = list(input())

if len(S) == 26:
    print(-1)
    exit()

for i in range(len(S)):
    S[i] = ord(S[i]) - 97

for i in range(26):
    if i not in S:
        S += [i]
        break

for i in range(len(S)):
    S[i] = chr(S[i] + 97)

print(*S, sep="")

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


s = input()
l = [chr(v) for v in range(ord("a"), ord("z")+1)]
l2 = {c:i for i,c in enumerate(l)}
def sub(s1, s2, used):
    if not s1:
        return -1
    if len(s1)+len(s2)<26:
        for i in range(26):
            if l[i] not in used:
                return s1+l[i]+s2
    else:
        if not s2:
            return sub(s1[:-1], s1[-1], set(s1[:-1]))
        for i in range(l2[s2[0]]+1, 26):
            if l[i] not in used:
                ans = s1+l[i]
#                 used.add(l[i])
#                 ans = ans + "".join(sorted([c for c in l if c not in used]))
                return ans
        used.remove(s1[-1])
        return sub(s1[:-1], s1[-1]+s2, used)
ans = sub(s, "", set(s))
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
A=[chr(i) for i in range(97, 97+26)]
N=len(S)

if N<26:
    for a in A:
        if a not in S:
            S+=a
            break
    print(S)

else:
    cond=True
    d=[-1]
    for i in range(N):
        n=A.index(S[-i-1])
        if n>max(d):
            d.append(n)
        else:
            ans=S[:N-i-1]+A[n+1]
            cond=False
            break
    
    if cond:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
A=[chr(i) for i in range(97, 97+26)]
N=len(S)

if N<26:
    d={}
    for s in S:
        d[s]=1
    for a in A:
        if a not in d:
            S+=a
            break
    print(S)

else:
    cond=True
    d=[-1]
    for i in range(N):
        n=A.index(S[-i-1])
        if n>max(d):
            d.append(n)
        else:
            ans=S[:N-i-1]+A[n+1]
            cond=False
            break
    
    if cond:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
A=[chr(i) for i in range(97, 97+26)]
N=len(S)
print(S)
if N<26:
    d={}
    for s in S:
        d[s]=1
    for a in A:
        if a not in d:
            S+=a
            break
    print(S)

else:
    cond=True
    d=[-1]
    for i in range(N):
        n=A.index(S[-i-1])
        if n>max(d):
            d.append(n)
        else:
            ans=S[:N-i-1]+A[n+1]
            cond=False
            break
    
    if cond:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
A=[chr(i) for i in range(97, 97+26)]
N=len(S)

if N<26:
    d={}
    for s in S:
        d[s]=1
    for a in A:
        if a not in d:
            S+=a
            break
    print(S)

else:
    cond=True
    d=[-1]
    for i in range(N):
        n=A.index(S[-i-1])
        if n>max(d):
            d.append(n)
        else:
            ans=S[:N-i-1]+A[n+1]
            cond=False
            break
    
    if cond:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
import string


def solution(S, min_c=None):
    if len(S) == 0:
        print(-1)
        return

    unique_char = set(S)
    try:
        for c in string.ascii_lowercase:
            if c not in unique_char and c > min_c:
                print(S + c)
                return
    except TypeError:
        for c in string.ascii_lowercase:
            if c not in unique_char:
                print(S + c)
                return
    solution(S[:-1], min_c=S[-1])


if __name__ == "__main__":

    S = input()
    unique_char = set(S)
    solution(S)

Submission status: Wrong Answer

Solution code:
import string


def solution(S, min_c="a"):
    if len(S) == 0:
        print(-1)
        return

    unique_char = set(S)
    for c in string.ascii_lowercase:
        if c not in unique_char and c > min_c:
            print(S + c)
            return
    solution(S[:-1], min_c=S[-1])


if __name__ == "__main__":

    S = input()
    unique_char = set(S)
    solution(S)

Submission status: Wrong Answer

Solution code:
import string
import sys

S = input()
unique_char = set(S)
for c in string.ascii_lowercase:
    if c not in unique_char:
        print(S + c)
        sys.exit(0)
print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S=list(input())
N=len(S)
abc=list("abcdefghijklmnopqrstuvwxyz")
D={}
for a in abc:
    D[a]=0
idx={}
for i in range(26):
    idx[abc[i]]=i

for s in S:
    D[s]+=1
L=[]
for k,v in D.items():
    if v==0:
        L.append(k)

L=sorted(L)
if len(L)==0:
    lis=[]
    for i in range(25,-1,-1):
        s=S[i]
        lis.append(s)
        if i!=0:
            if idx[s]>idx[S[i-1]]:
                break
    if len(lis)==26:
        print(-1)
        exit()

    lis=sorted(lis)
    ans="".join(S[:i-1])+lis[0]
    print(ans)

        
else:
    print("".join(S)+L[0])

Submission status: Wrong Answer

Solution code:
S=list(input())

abc=list("abcdefghijklmnopqrstuvwxyz")
D={}
for a in abc:
    D[a]=0

for s in S:
    D[s]+=1
L=[]
for k,v in D.items():
    if v==0:
        L.append(k)

L=sorted(L)
if len(L)==0:
    print(-1)
else:
    print("".join(S)+L[0])

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

if L != 26:
  # まだ使ってないもので、最大ものもを最後につける
  USED = [False] * 26
  for s in S:
    USED[ord(s) - 97] = True
  
  for i in range(26):
    if not USED[i]:
      S.append(chr(97+i))
      break
  
  print("".join(S))
else:
  print("a")

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
S=input()
N=len(S)
if N!=26:
    for i in range(26):
        c=chr(97+i)
        if not c in S:
            print(S+c)
            exit()
li=[]
for i in reversed(range(26)):
    li.append(S[i])
    if i==0:
        print(-1)
        exit()
    if S[i-1]<S[i]:
        e=i-1
        break
li.sort()
print(S[:e]+li[0])

Submission status: Wrong Answer

Solution code:
def dfs(T,s):
    if T=='':
        return -1
    for i in range(26):
        c=chr(97+i)
        if T+c>S and not c in s:
            return T+c
    ns=s.copy()
    ns.remove(T[-1])
    return dfs(T[:-1],ns)

S=input()
s=set(S)
print(dfs(S,s))

Submission status: Wrong Answer

Solution code:
s=input()
s1=list(s)
num=list(range(97,123))
if len(s1)<26:
    for i in range(len(s1)):
        num.remove(ord(s1[i]))
    ans=s+chr(num[0])
else:
    if s=="zyxwvutsrqponmlkjihgfedcba":
        ans=-1
    else:
        num2=[]
        for i in range(26):
            if ord(s1[-1-i])<ord(s1[-2-i]):
                num2.append(ord(s1[-1-i]))
            else:
                num2.append(ord(s1[-1-i]))
                break
        ans=s[:25-len(num2)]+chr(min(num2))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

S = input()

alphas = [chr(ord('a') + i) for i in range(26)]

if len(S) < 26:
    remain = set(alphas) - set(S)
    remain = sorted(remain)
    print(S + remain[0])
else:
    pre = S[-1]
    i = 2
    while i < len(S):
        if pre < S[len(S) - i]:
            pre = S[len(S) - i]
            i += 1
        else:
            break
    if i == 26:
        print(-1)
    else:
        front = S[:-i]
        back = sorted(S[-i:])
        to_be_changed = S[-i]
        for c in back:
            if c > to_be_changed:
                print(front + c)
                break

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

S = input()

alphas = [chr(ord('a') + i) for i in range(26)]

if len(S) < 26:
    remain = set(alphas) - set(S)
    remain = sorted(remain)
    print(S + remain[0])
else:
    pre = S[-1]
    i = 1
    while i < len(S):
        if pre <= S[len(S) - i]:
            pre = S[len(S) - i]
            i += 1
            continue
        break
    if i == 26:
        print(-1)
    else:
        front = S[:-i]
        back = sorted(S[-i:])
        to_be_changed = S[-i]
        for c in back:
            if c > to_be_changed:
                print(front + c)
                break

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

S = input()

alphas = [chr(ord('a') + i) for i in range(26)]

if len(S) < 26:
    remain = set(alphas) - set(S)
    remain = sorted(remain)
    print(S + remain[0])
else:
    pre = S[-1]
    i = 1
    while i < len(S):
        if pre <= S[len(S) - i]:
            i += 1
            continue
        break
    if i == 26:
        print(-1)
    else:
        front = S[:-i]
        back = sorted(S[-i:])
        to_be_changed = S[-i]
        for c in back:
            if c > to_be_changed:
                print(front + c)
                break

Submission status: Wrong Answer

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

if n!=26:
  for i in [chr(ord('a') + i) for i in range(26)]:
    if not i in s:
      tmp=i
      break
  s.append(tmp)
  print("".join(s))

elif n==26:
  if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)

  else:
    for j in range(n)[::-1]:
      for k in range(1,122-ord(s[j])+1):
        tmp = chr(ord(s[j])+k)
        if tmp not in s[:j]:
          s[j]=tmp
          del s[j+1:]
          print("".join(s))
          exit()

Submission status: Wrong Answer

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
a = [0]*27

for x in s:
    a[ord(x)-ord('a')] += 1

for i in range(27):
    if a[i] == 0:
        tmp = i
        break

if tmp != 26:
    print(s + chr(ord('a')+tmp))
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        for i in range(len(s)-1,-1,-1):
            if s[i] < s[len(s)-1]:
                s = s[:i] + s[len(s)-1]
                break
        print(s)

Submission status: Wrong Answer

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
a = [0]*27

for x in s:
    a[ord(x)-ord('a')] += 1

for i in range(27):
    if a[i] == 0:
        tmp = i
        break

if tmp != 26:
    print(s + chr(ord('a')+tmp))
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        tmp = s[len(s)-1]
        for i in range(len(s)-1,-1,-1):
            if s[i] < tmp:
                s = s[:i] + tmp
                break
            else:
                tmp = min(s[i],tmp)
        print(s)

Submission status: Wrong Answer

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
a = [0]*27

for x in s:
    a[ord(x)-ord('a')] += 1

for i in range(27):
    if a[i] == 0:
        tmp = i
        break

if tmp != 26:
    print(s + chr(ord('a')+tmp))
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        tmp = s[len(s)-1]
        for i in range(len(s)-1,-1,-1):
            if s[i] < tmp:
                s = s[:i-1] + tmp
                break
            else:
                tmp = min(s[i],tmp)
        print(s)

Submission status: Wrong Answer

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
a = [0]*27

for x in s:
    a[ord(x)-ord('a')] += 1

for i in range(27):
    if a[i] == 0:
        tmp = i
        break

if tmp != 26:
    print(s + chr(ord('a')+tmp))
else:
    if s == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        tmp = s[len(s)-1]
        for i in range(len(s)-1,-1,-1):
            if s[i] < s[len(s)-1]:
                s = s[:i-1] + tmp
                break
            else:
                tmp = min(s[i],tmp)
        print(s)

Submission status: Wrong Answer

Solution code:
S=input()
if len(S)<26:
    for char in range(ord("a"),ord("z")):
        if chr(char) not in S:
            break
    print(S+chr(char))
else:
    stack=list(S[-1])
    for s in S[len(S)-2::-1]:
        if s>stack[-1]:
            stack.append(s)
        else:
            break
    if len(stack)==26:
        print(-1)
    else:
        print(S[0:len(S)-len(stack)-1]+sorted(stack)[0])

Submission status: Wrong Answer

Solution code:
S=input()
if len(S)<26:
    for char in range(ord("a"),ord("z")):
        if chr(char) not in S:
            break
    print(S+chr(char))
else:
    stack=list(S[-1])
    for s in S[len(S)-2::-1]:
        if s>stack[-1]:
            stack.append(s)
        else:
            break
    if len(stack)==26:
        print(-1)
    else:
        print(S[0:len(S)-len(stack)-1]+sorted(stack)[-1])

Submission status: Wrong Answer

Solution code:
S=input()
if len(S)<26:
    print(S+chr(ord(sorted(S)[0])+1))
else:
    stack=list(S[-1])
    for s in S[len(S)-2::-1]:
        if s>stack[-1]:
            stack.append(s)
        else:
            break
    if len(stack)==26:
        print(-1)
    else:
        print(S[:-len(stack)-1]+min(s for s in stack if s>S[-len(stack)-1]))

Submission status: Wrong Answer

Solution code:
S = input()

alpha = 'abcdefghijklmnopqrstuvwxyz'
# len(S)が26未満の場合
if len(S) < 26:
    for char in alpha:
        # Sに入っていない文字の中で最小のものを末尾に足す
        if char not in S:
            print(S + char)
            exit()

# len(S)が26の場合
else:
    arr = [S[-1]]

    for i in range(1, 26):
        l = 25 - i
        if max(arr) > S[l]:
            chars = []

            for c in arr:
                # 下のifがよくわかってない
                # bcdefghijklmnopqrstuvwx | zya　みたいな時のため？
                # ifがないと~~~vwa とか吐いて前に戻ってしまうっぽい
                # 時間内に思いつく気が全くしない
                if c > S[l]:
                    chars.append(c)
            char = min(chars)
            print(chars)

            # for c in arr:
            #     chars.append(c)
            # char = min(chars)

            print(S[:l] + char)
            exit()
        else:  # max(sr)>S[l]　の場合
            arr.append(S[l])

    print(-1)

Submission status: Wrong Answer

Solution code:
S = input()

alpha = 'abcdefghijklmnopqrstuvwxyz'
# len(S)が26未満の場合
if len(S) < 26:
    for char in alpha:
        # Sに入っていない文字の中で最小のものを末尾に足す
        if char not in S:
            print(S + char)
            exit()

# len(S)が26の場合
else:
    arr = [S[-1]]

    for i in range(1, 26):
        l = 25 - i
        if max(arr) > S[l]:
            chars = []

            # for c in arr:
            #     if c > S[l]:
            #         chars.append(c)
            # char = min(chars)

            for c in arr:
                chars.append(c)
            char = min(chars)

            print(S[:l] + char)
            exit()
        else:  # max(sr)>S[l]　の場合
            arr.append(S[l])

    print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
text = 'abcdefghijklmnopqrstuvwxyz'
if len(s) < 26:
  for c in text:
    if c not in s:
      print(s + c)
      break
else:
  cand = {s[-1]}
  for i in range(1, len(s)):
    j = len(s) - i - 1
    if max(cand) > s[j]:
      print(s[:j] + min(c for c in cand if c > s[j]))
      break
  else:
    print(-1)

Submission status: Wrong Answer

Solution code:
S = input()
a = []
b = ["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 s in S:
  if s not in a:
    a.append(s)
a.sort()
a.append(0)
if len(a) == 27:
  print(-1)
  exit()
else:
  for i in range(len(a)):
    if a[i] != b[i]:
      if a[-1] == "z":
        print(b[i])
        exit()
      else:
        print(S+b[i])
        exit()

Submission status: Wrong Answer

Solution code:
S = input()
a = []
b = ["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 s in S:
  if s not in a:
    a.append(s)
a.sort()
a.append(0)
if len(a) == 27:
  print(-1)
  exit()
else:
  for i in range(len(a)):
    if a[i] != b[i]:
      print(S+b[i])
      exit()

Submission status: Wrong Answer

Solution code:
S = input()
A = []
b = ["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 s in S:
  if s not in A:
    A.append(s)
A.sort()
A.append(0)
if len(A) == 27:
  print(-1)
else:
  for i in range(len(A)+1):
    if A[i] != b[i]:
      print(S+b[i])
      exit()

Submission status: Wrong Answer

Solution code:
S = input()
a = []
b = ["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 s in S:
  if s not in a:
    a.append(s)
a.sort()
if len(a) == 26:
  print(-1)
else:
  for i in range(len(a)):
    if a[i] != b[i]:
      print(S+b[i])
      exit()

Submission status: Wrong Answer

Solution code:
S = input()
a = []
b = ["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 s in S:
  if s not in a:
    a.append(s)
a.sort()
if len(a) == 0:
  print(-1)
else:
  for i in range(len(a)):
    if a[i] != b[i]:
      print(S+a[i])
      exit()

Submission status: Wrong Answer

Solution code:
s = list(input())

li = [chr(i) for i in range(97, 97+26)]
temp = []
cnt = 0
for i in li:
    if i in s:
        continue
    else:
        s.append(i)
        break
else:
    for i in range(1,27):
        if i == 1:
            check = ord(s[-i])
            continue
        
        if check > ord(s[-i]):
            print("".join(s[:26-i]) + s[-1])
            exit()
    else:
        print(-1)
        exit()

print("".join(s))

Submission status: Wrong Answer

Solution code:
s = list(input())

li = [chr(i) for i in range(97, 97+26)]
temp = []
cnt = 0
for i in li:
    if i in s:
        continue
    else:
        s.append(i)
        break
else:
    ans = []
    for i in range(26):
        if li[i] == s[i]:
            ans.append(li[i])
        else:
            try:
                for j in range(97+i, 97+26):
                    if chr(j) in temp:
                        continue
                    ans[-1] = chr(97+i)
                    if cnt == 0:
                        print("".join(ans))
                        cnt += 1
                        exit()
            except:
                temp.append(li[i])
    else:
        if cnt == 0:
            print(-1)
            exit()

if cnt == 0:
    print("".join(s))

Submission status: Wrong Answer

Solution code:
s = input()
if len(s) != 26:
    x = sorted(set(map(chr, range(97, 123)))-set(s))
    print(s+x[0])
else:
    num = list(map(lambda x: ord(x)-ord('a'), s))
    for i in reversed(range(25)):
        L, R = i, i+1
        if num[L] < num[R]:
            tmp = 26
            for r in num[R:]:
                if num[L] < r:
                    tmp = min(tmp, r)
            ans = num[:L]+[tmp]
            print(ans)
            print(''.join(map(lambda x: chr(x+ord('a')), ans)))
            break
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
if len(s) < 26:
  for i in range(97, 124):
    if chr(i) not in s:
      break
  print(s + chr(i))
else:
  prep = [s[-1]]
  for i in s[-2::-1]:
    if i > prep[-1]:
      prep.append(i)
    else:
      break
  if len(prep) == 26:
    print(-1)
  else:
    print(s[:-len(prep)-1] + min(prep))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def next_vary(S):
    if len(S) < 26:
        alpha = [0] * 26
        for s in S:
            alpha[ord(s) - 97] += 1
        add = None
        for i in range(26):
            if alpha[i] == 0:
                add = chr(i + 97)
                break
        S.append(add)
        return ''.join(S)
    
    else:
        before = set(S)
        for i in range(len(S) - 1,-1,-1):
            s = S[i]
            if s == 'z':
                before.discard('z')
                S.pop()
            else:
                if chr(ord(s) + 1) in before:
                    before.discard(s)
                    S.pop()
                else:
                    S[i] = chr(ord(s) + 1)
                    break
        return ''.join(S)

def main():
    S = input()
    if S == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
        return
    
    ans = next_vary(list(S))
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()

if len(S) < 26:
    for i in range(26):
        if chr(i+97) not in S:
            ans = S + chr(i+97)
            break
elif S == "zyxwvutsrqponmlkjihgfedcba":
    ans = -1
else:
    ans = ""
    m = S[-1]
    for i in range(24, -1, -1):
        if S[i+1] > S[i] > m:
            for j in range(i):
                ans += S[j]
            ans += S[i+1]
            break
        if S[i] < m:
            for j in range(i):
                ans += S[j]
            ans += m
            break
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()

if len(S) < 26:
    for i in range(26):
        if chr(i+97) not in S:
            ans = S + chr(i+97)
            break
elif S == "zyxwvutsrqponmlkjihgfedcba":
    ans = -1
else:
    ans = ""
    m = S[-1]
    for i in range(24, -1, -1):
        if S[i] < m:
            for j in range(i):
                ans += S[j]
            ans += m
            break
print(ans)