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=list(input())
N=len(S)
alphabet="abcdefghijklmnopqrstuvwxyz"
Appeared,A_to_N={},{}
for i in range(26):
  Appeared[alphabet[i]]=False
  A_to_N[alphabet[i]]=i
if N<26:
  for i in range(N):
    Appeared[S[i]]=True
  for i in range(26):
    if not Appeared[alphabet[i]]:
      S.append(alphabet[i])
      print("".join(S))
      exit()

last=A_to_N[S[-1]]
a=[S[-1]]
for i in range(N-1)[::-1]:
  if A_to_N[S[i]]>last:
    a.append(S[i])
    last=A_to_N[S[i]]
  else:
    for k in a:
      if A_to_N[S[i]]<A_to_N[k]:
        S[i]=k
        print("".join(S[:i+1]))
        exit()
print(-1)

Submission status: Accepted

Solution code:
S = input()
chr_list = [1] * 26

if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for s in S:
        chr_list[ord(s) - ord("a")] = 0

    if all(map(lambda c: c == 0, chr_list)):
        c_set = [S[-1]]
        for i in range(24, -1, -1):
            if not ord(S[i]) > ord(S[i+1]):
                c_set = [c for c in c_set if c >= S[i]]
                print(S[:i] + min(c_set))
                break
            c_set.append(S[i])
    else:
        for i, v in enumerate(chr_list):
            if v == 1:
                print(S + chr(ord("a") + i))
                break

Submission status: Accepted

Solution code:
S = input()
if len(S) < 26:
    for i in range(26):
        c = chr(ord('a') + i)
        if c in S:
            continue
        print(S + c)
        exit()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit()

s = []
for i in range(24,-1,-1):
    if S[i] > S[i+1]:
        s.append(S[i+1])
        continue
    else:
        s.append(S[i+1])
        last = 'z'
        for c in s:
            if c < S[i]: continue
            last = min(last, c)
        print(S[:i] + last)
        exit()

Submission status: Accepted

Solution code:
S = input()
l = len(S)
if l != 26:
    colors = [0]*26
    for i in range(l):
        colors[ ord(S[i]) - ord('a')] = 1
    for i in range(26):
        if colors[i] == 0:
            ans = S + chr( i + ord('a'))
            break
    print(ans)
elif S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for i in range(25,0,-1):
        if S[i] > S[i-1]:
            now = i
            break
    colors = [0]*26
    for i in range(now-1):
        colors[ ord(S[i]) - ord('a')] = 1
    c = 'z'
    for i in range(now-1,26):
        if S[now-1] < S[i]:
            c = min(c, S[i])
    print(S[:now-1]+c)

Submission status: Accepted

Solution code:
S=input()
flag = False
if S=="zyxwvutsrqponmlkjihgfedcba":
    T="-1"
elif len(S)<=25:
    for i in range(97,123):
        if chr(i) not in S:
            T=S+chr(i)
            break
else:
    for i in range(2,27):
        for j in range(ord(S[-i])+1,123):
            if chr(j) not in S[:-i]:
                T=S[:-i]+chr(j)
                flag=True
                break
        if flag:
            break
print(T)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
abc = list('abcdefghijklmnopqrstuvwxyz')
if N < 26:
    for c in abc:
        if c not in S:
            print(S + c)
            exit()


def dfs(s):
    if s > S:
        print(s)
        exit()
    elif s == S[:len(s)]:
        for c in abc:
            if c not in s:
                dfs(s + c)


dfs('')
print(-1)

Submission status: Accepted

Solution code:
s=input()
l=[0]*26
for i in s:
    l[ord(i)-ord("a")]=1
if len(s)<26:
    for i in range(26):
        if l[i]==0:
            print(s+chr(i+ord("a")))
            exit()
else:
    for i in range(26):
        l[ord(s[-i-1])-ord("a")]=0
        for j in range(26):
            if l[j]==0 and ord(s[-i-1])-ord("a")<j:
                print(s[:-i-1]+chr(j+ord("a")))
                exit()
print(-1)

Submission status: Accepted

Solution code:
import sys


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


def next_permutation(l: list) -> bool:
    n = len(l)
    i = n - 2
    while i >= 0 and l[i] >= l[i + 1]:
        i -= 1

    if i == -1:
        return False

    j = i + 1
    while j < n and l[j] > l[i]:
        j += 1
    j -= 1

    l[i], l[j] = l[j], l[i]

    left = i + 1
    right = n - 1

    while left < right:
        l[left], l[right] = l[right], l[left]
        left += 1
        right -= 1

    return True


def main() -> None:
    s = next_str()
    t = [a for a in s]

    for v in range(ord('a'), ord('z') + 1):
        if chr(v) not in s:
            print(s + chr(v))
            return

    if not next_permutation(t):
        print(-1)
    else:
        i = 0
        while s[i] == t[i]:
            print(s[i], end="")
            i += 1
        print(t[i])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
alpo = 'abcdefghijklmnopqrstuvwxyz'
alp = [False for _ in range(26)]
if len(S) < 26:
    for i in S:
        alp[ord(i)-97] = True
    for k,j in enumerate(alp):
        if not j:
            ans = S + chr(k+97)
            break
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    candi = [0]
    for i in range(26):
        if ord(S[-1-i]) < max(candi):
            for j in sorted(candi):
                if j > ord(S[-1-i]):
                    ans = S[:25-i]+chr(j)
                    break
            break
        candi.append(ord(S[-1-i]))
print(ans)

Submission status: Accepted

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
        print("-1")
        exit()
S = list(S)
S = [ord(e) for e in S]
ord_list = list(range(97,123))
# アルファベットが26個含まれているかチェック
for e in ord_list:
        if not e in S:
                S.append(e)
                S = [chr(e) for e in S]
                print("".join(S))
                exit()
#以下はアルファベット26個確定　後ろから最適になるように文字を置換していく
for i in range(len(S)-2,-1,-1):
        note = 123
        for j in range(i+1,len(S)):
                if S[i] < S[j] and S[j] < note:
                        note = S[j]
        if note < 123:
                S[i] = note
                del S[i+1:]
                S = [chr(e) for e in S]
                print("".join(S))
                break

Submission status: Accepted

Solution code:
S = input()

abc = 'abcdefghijklmnopqrstuvwxyz'

less_abc = abc

for s in S:
    less_abc = less_abc.replace(s,'')

ans = ''
if len(S) != 26:
    ans = S + less_abc[0]
elif S == 'zyxwvutsrqponmlkjihgfedcba':
    ans = -1
else:
    bef_tmp = -1
    tmp = ''
    for s in S[::-1]:
        ind = abc.index(s)
        if ind > bef_tmp:
            bef_tmp = ind
            tmp += s
        else:
            ans = S[:S.index(s)]

            tar_s = s
            ind = abc.index(tar_s)
            for ss in abc[ind:]:
                if ss in tmp:
                    break

            ans += ss
            break

    # long_now = ''
    # long_s = ''
    # num = 0
    # ind = 27
    # for s in S:
    #     ind_now = abc.index(s)
    #     if ind > ind_now:
    #         ind = ind_now
    #         long_now += s
    #     else:
    #         if len(long_s) <= len(long_now):
    #             long_s = long_now

    #         long_now = s
    #         ind = ind_now

    # if len(long_s) <= len(long_now):
    #     long_s = long_now

    # ind = S.index(long_s)
    # S = S[:ind]
    # for s in abc[abc.index(S[-1])+1:]:
    #     if s in long_s:
    #         ans = S + s
    #         break

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()
x = list("abcdefghijklmnopqrstuvwxyz")

if len(s)<26:
    for c in x:
        if c not in s:
            print(s+c)
            exit()
else:
    s = list(s)
    d = set()
    c = s[-1]
    d.add(c)
    i = len(s)-2
    while True:
        if i<0:
            break
        elif s[i]>c:
            c = s[i]
            i -= 1
            d.add(c)
        else:
            break
    if i>=0:
        minc = None
        for c in d:
            if c<s[i]:
                continue
            elif minc is None:
                minc = c
            elif c<minc:
                minc = c
        if minc is not None:
            s[i] = minc
            s = s[:i+1]
            print("".join(s))
            exit()
print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1]  # s = s_in()


ALPHABET = [i for i in range(ord('a'), ord('z') + 1)]
S = s_in()

if len(S) < len(ALPHABET):
    list_s = list(S)
    for a in ALPHABET:
        if chr(a) not in list_s:
            S += chr(a)
            break
    print(S)
else:
    reversed_s = list(S)[::-1]
    last_s = reversed_s[0]
    cand = [ord(last_s)]
    ans = ''
    for i in range(len(reversed_s[1:])):
        s = reversed_s[i + 1]
        if ord(s) > ord(last_s):
            last_s = s
            cand.append(ord(last_s))
        else:
            min_s = float('inf')
            for c in cand:
                if c > ord(s):
                    min_s = min(min_s, c)
            if (i + 2) == len(reversed_s):
                ans = chr(min_s)
            else:
                ans += ''.join(reversed_s[i + 2:][::-1]) + chr(min_s)
            break
    if ans == '':
        print(-1)
    else:
        print(ans)

Submission status: Accepted

Solution code:
S = input()
AB = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        out, Fin = [], False
        for i in range(26):
            out.append(AB.index(S[-1]))
            out.sort()
            S = S[:len(S)-1]
            for o in out:
                if AB.index(S[-1]) < o:
                    S = S[:len(S)-1] + AB[o]
                    print(S)
                    Fin = True
                    break
            if Fin:
                break
else:
    for i in AB:
        if i not in set(S):
            S += i
            print(S)
            break

Submission status: Accepted

Solution code:
S = input()
if len(S) < 26:
    for i in range(26):
        if chr(ord('a')+i) not in S:
            print(S + chr(ord('a')+i))
            exit()
for i in range(len(S)-1, -1, -1):
    minC = ord('z')+1
    for j in range(len(S)-1, i, -1):
        if ord(S[j]) > ord(S[i]):
            minC = min(minC, ord(S[j]))
    if minC < ord('z')+1:
        print(S[:i] + chr(minC))
        exit()
print(-1)

Submission status: Accepted

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

usable = [not a in S for a in "abcdefghijklmnopqrstuvwxyz"]

for i in range(len(usable)):
    if usable[i]:
        print(S + chr(ord('a') +i))
        exit()
else:
    while True:
        poped_char = S[-1]
        usable[ord(poped_char) - ord('a')] = True
        S = S[:-1]
        # print(S, poped_char, usable)
        for i in range(ord(poped_char)-ord('a')+1,len(usable)):
            if usable[i]:
                print(S + chr(ord('a') +i))
                exit()

Submission status: Accepted

Solution code:
import sys

def forward(s1, c):
    for s in s1:
        if s > c:
            return False
    return True

def get_char(s, c):
    return sorted(list(filter(lambda x: x > c, s)))[0]
    
S = sys.stdin.readline().strip()

c_list = [chr(i) for i in range(97, 97+26)]
#num_list = [int(chr(s)) for s in S]
s_list = [s for s in S]
all_char = "abcdefghijklmnopqrstuvwxyz"

if S == "zyxwvutsrqponmlkjihgfedcba":
    print("-1")
elif len(S) != 26:
    for c in c_list:
        if c not in s_list:
            print(S + c)
            break
else:
    for i in reversed(list(range(25))):
        sub = S[i+1:]
        if forward(sub, S[i]):
            # S[i]が最大
            pass
        else:
            #m = max(sub)
            m = get_char(sub, S[i])
            print(S[:i] + m)
            break

Submission status: Accepted

Solution code:
n = raw_input()

aset = set(n)

alphabets = set("abcdefghijklmnopqrstuvwxyz")

while True:
    if len(n) == 0:
        print -1
        break

    c = n[-1]
    if c == 'z':
        n = n[:-1]
        continue

    if len(aset) != 26:
        print n + min(list(alphabets - aset))
        break

    _next = chr(ord(c)+1)
    n = n[:-1]+_next
    if len(list(set(n))) == len(n):
        print n
        break

Submission status: Accepted

Solution code:
S = input()

if len(S) == 26:
    tmp = S[:-1]
    f = True
    removed = [S[-1]]
    while len(tmp) > 0:
        removed.sort()
        for r in removed:
            if tmp[-1] < r:
                print(tmp[:-1] + r)
                exit()
        removed.append(tmp[-1])
        tmp = tmp[:-1]


alphabet = ["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"]
words = set(alphabet)
input_words = set()

for s in S:
    input_words.add(s)

margin_word = words - input_words
margin_word = list(margin_word)
margin_word.sort()

if len(margin_word) == 0:
    print(-1)
else:
    print(S + margin_word[0])

Submission status: Accepted

Solution code:
s=input()
l=[chr(i) for i in range(97,97+26)]
if len(s)!=26:
  for i in l:
    if i not in s:
      print(s+i)
      break
else:
  for j in range(26):
    S=s[j:26]
    c=0
    for k in range(len(S)-1):
      if S[k]>S[k+1]:c+=1
    if c==len(S)-1:
      break
  if j==0:print(-1)
  else:
    for m in range(l.index(s[j-1])+1,26):
      if l[m] not in s[0:j-1]:
        x=l[m]
        break
    print(s[0:j-1]+x)

Submission status: Accepted

Solution code:
s=input()
l=[chr(i) for i in range(97,97+26)]
if len(s)!=26:
  for i in l:
    if i not in s:
      print(s+i)
      break
else:
  for j in range(26):
    S=s[j:26]
    c=0
    for k in range(len(S)-1):
      if S[k]>S[k+1]:c+=1
    if c==len(S)-1:
      x=j
      break
  if x==0:print(-1)
  else:
    for m in range(l.index(s[x-1])+1,26):
      if l[m] not in s[0:x-1]:
        y=l[m]
        break
    print(s[0:x-1]+y)

Submission status: Accepted

Solution code:
s = input()
for i in range(97, 97+26):
    if chr(i) not in s:
        print(s + chr(i))
        exit()
a = set()
for i in range(24, -1, -1):
    if s[i] > s[i+1]:
        continue
    else:
        for j in range(ord(s[i])-97+1,26):
            a.add(chr(j+97))
        temp = min(a & set(s[i+1:]))
        print(s[:i] + temp)
        exit()
print(-1)

Submission status: Accepted

Solution code:
s=input()
x=[0 for _ in range(26)]
if len(s)<26:
    for i in range(len(s)):
        x[ord(s[i])-ord('a')]=1
    for j in range(26):
        if x[j]==0:
            k=j
            break
    print("".join([s,chr(ord('a')+k)]))
elif s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
else:
    for i in range(2,27):
        if s[-i]<s[-i+1]:
            break
    for j in sorted(s[-i+1:]):
        if j>s[-i]:
            break
    print(s[:-i]+j)

Submission status: Accepted

Solution code:



def alphabet_to_int(c):
    if c=="a":
        return 0
    elif c=="b":
        return 1
    elif c=="c":
        return 2
    elif c=="d":
        return 3
    elif c=="e":
        return 4
    elif c=="f":
        return 5
    elif c=="g":
        return 6
    elif c=="h":
        return 7
    elif c=="i":
        return 8
    elif c=="j":
        return 9
    elif c=="k":
        return 10
    elif c=="l":
        return 11
    elif c=="m":
        return 12        
    elif c=="n":
        return 13
    elif c=="o":
        return 14
    elif c=="p":
        return 15
    elif c=="q":
        return 16
    elif c=="r":
        return 17
    elif c=="s":
        return 18
    elif c=="t":
        return 19
    elif c=="u":
        return 20
    elif c=="v":
        return 21
    elif c=="w":
        return 22
    elif c=="x":
        return 23
    elif c=="y":
        return 24              
    elif c=="z":
        return 25        
        
def int_to_alphabet(i):
    if i==0:
        return "a"
    elif i==1:
        return "b"
    elif i==2:
        return "c"
    elif i==3:
        return "d"
    elif i==4:
        return "e"
    elif i==5:
        return "f"
    elif i==6:
        return "g"
    elif i==7:
        return "h"
    elif i==8:
        return "i"
    elif i==9:
        return "j"
    elif i==10:
        return "k"
    elif i==11:
        return "l"
    elif i==12:
        return "m"        
    elif i==13:
        return "n"
    elif i==14:
        return "o"
    elif i==15:
        return "p"
    elif i==16:
        return "q"
    elif i==17:
        return "r"
    elif i==18:
        return "s"
    elif i==19:
        return "t"
    elif i==20:
        return "u"
    elif i==21:
        return "v"
    elif i==22:
        return "w"
    elif i==23:
        return "x"
    elif i==24:
        return "y"              
    elif i==25:
        return "z"        
            
        
S=raw_input()

if len(S)<=25:
    char_set = [0 for i in range(26)]
    for i in range(len(S)):
        char_set[alphabet_to_int(S[i])]=1
    for i in range(26):
        if char_set[i]==0:
            ans=S+int_to_alphabet(i)
            print ans
            break
else:
    prev_char=S[-1]
    prev_set = [0 for i in range(26)]
    prev_set[alphabet_to_int(prev_char)]=1
    for i in range(26):
        if alphabet_to_int(prev_char)>alphabet_to_int(S[-(i+1)]):
            for j in range(alphabet_to_int(S[-(i+1)]),26):
                if prev_set[j] == 1:
                        ans=S[:-(i+1)]+int_to_alphabet(j)
                        break
            print ans
            break
        if i==25:
            print -1
        prev_set[alphabet_to_int(S[-(i+1)])]=1
        prev_char=S[-(i+1)]

Submission status: Accepted

Solution code:
s=raw_input()
for i in range(ord('a'), ord('a')+26):
	if s.find(chr(i))==-1:
		print s+chr(i)
		exit(0)
for i in range(25, 0, -1):
	if s[i]>s[i-1]:
		print s[0:i-1]+min([ch for ch in s[i:] if ch>s[i-1]])
		quit()
assert s=='zyxwvutsrqponmlkjihgfedcba'
print -1

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def isGreater(x, y):
    # x > y or not
    if x.find(y) == 0:
        return True
    for xi, yi in zip(x, y):
        if xi == yi:  continue
        else: 
            if xi > yi: return True
    return False


S = input()

alpha = set([chr(ord("a") + i) for i in range(26)])
S_1 = "".join([chr(ord("a") + i) for i in reversed(range(26))]) # -1

if len(set(S)) < 26:
    al = sorted(list((alpha - set(S))))
    print(S + al[0])
elif S == S_1:
    print(-1)
else:
    for i in reversed(range(len(S) -1 )):
        for a in sorted(list(alpha - set(S[:i]))):
            S2 = S[:i] + a
            if isGreater(S2, S):
                print(S2)
                sys.exit()

Submission status: Accepted

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

Submission status: Accepted

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

S = input()

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

Submission status: Accepted

Solution code:
# cook your dish here
s=list(input())
k = 0
if s.count('a') == 0:
    s += 'a'
elif s.count('b') == 0:
    s += 'b'
elif s.count('c') == 0:
    s += 'c'
elif s.count('d') == 0:
    s += 'd'
elif s.count('e') == 0:
    s += 'e'
elif s.count('f') == 0:
    s += 'f'
elif s.count('g') == 0:
    s += 'g'
elif s.count('h') == 0:
    s += 'h'
elif s.count('i') == 0:
    s += 'i'
elif s.count('j') == 0:
    s += 'j'
elif s.count('k') == 0:
    s += 'k'
elif s.count('l') == 0:
    s += 'l'
elif s.count('m') == 0:
    s += 'm'
elif s.count('n') == 0:
    s += 'n'
elif s.count('o') == 0:
    s += 'o'
elif s.count('p') == 0:
    s += 'p'
elif s.count('q') == 0:
    s += 'q'
elif s.count('r') == 0:
    s += 'r'
elif s.count('s') == 0:
    s += 's'
elif s.count('t') == 0:
    s += 't'
elif s.count('u') == 0:
    s += 'u'
elif s.count('v') == 0:
    s += 'v'
elif s.count('w') == 0:
    s += 'w'
elif s.count('x') == 0:
    s += 'x'
elif s.count('y') == 0:
    s += 'y'
elif s.count('z') == 0:
    s += 'z'
else:
    for i in range(25):
        if s[25-i] > s[25-i-1]:
            r = s[25-i]
            for j in range(i+1):
                if s[25-i+j] > s[25-i-1]:
                    r = s[25-i+j]
            s = s[:25-i-1:] + list(r)
            break
        else:
            k += 1
if k == 25:
    print(-1)
else:
    ass = ''
    for i in range(len(s)):
        ass += s[i]
    print(ass)

Submission status: Accepted

Solution code:
from string import ascii_lowercase
s = input()
if len(s) != 26:
    nokori = set(ascii_lowercase) - set(s)
    print(s + min(nokori))
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
        if i == 26:
            print(-1)
            exit()
    s_ = sorted(s[-i + 1:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i]+j)

Submission status: Accepted

Solution code:
s=input()
a=list("abcdefghijklmnopqrstuvwxyz")
if len(s)!=26:nokori=sorted(set(a)-set(s),key=a.index);print(s+nokori[0])
else:
 for i in range(2,27):
  if s[-i]<s[-i+1]:
    break
  if i==26:
   print(-1)
   exit()
 t=sorted(s[-i+1:])
 for j in t:
  if j>s[-i]:break
 print(s[:-i]+j)

Submission status: Accepted

Solution code:
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
        if i == 26:
            print(-1)
            exit()
    s_ = sorted(s[-i + 1:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i] + j)

Submission status: Accepted

Solution code:
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
    s_ = sorted(s[-i + 1:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i] + j)

Submission status: Accepted

Solution code:
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(2, 27):
        if s[-i] <= s[-i + 1]:
            break
    s_ = sorted(s[-i:])
    for j in s_:
        if j > s[-i]:
            break
    print(s[:-i] + j)

Submission status: Accepted

Solution code:
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(1, 27):
        if s[-i] > s[-i - 1]:
            break
    s_ = sorted(s[-i:])
    for j in s_:
        if j > s[-i - 1]:
            break
    print(s[:-i - 1] + j)

Submission status: Accepted

Solution code:
s = input()
a = list("abcdefghijklmnopqrstuvwxyz")
if s == "zyxwvutsrqponmlkjihgfedcba":
     print(-1)
elif len(s) != 26:
    nokori = set(a) - set(s)
    nokori = sorted(nokori, key=a.index)
    print(s + nokori[0])
else:
    for i in range(1, 27):
        if list(s)[-i] > list(s)[-(i + 1)]:
            break
    T = sorted(list(s)[-i:])
    #print(T)
    for t in T:
        if t > list(s)[-(i + 1)]:
            break
    print("".join(list(s)[:-(i + 1)]) + t)

Submission status: Accepted

Solution code:
def solv(S):
    c = []
    for i in range(25, 0, -1):
        for j in range(i-1, -1, -1):
            if S[i] > S[j]:
                c.append(S[:j]+S[i])
    print(sorted(c)[0])
    return


alph = "abcdefghijklmnopqrstuvwxyz"
S = input()
for c in S:
    alph = alph.replace(c, "")
if alph != "":
    print(S+alph[0])
else:
    alph = "abcdefghijklmnopqrstuvwxyz"
    if S == alph[::-1]:
        print(-1)
    else:
        solv(S)

Submission status: Accepted

Solution code:
s = input()

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

def youngest(str):
    cnt = [0]*26
    for c in str:
        cnt[ord(c)-ord('a')] += 1
    return chr(ord('a')+cnt.index(0))

if len(s)<26:
    print(s+youngest(s))
    exit()

# len(s)==26
suffix = ''
for i,c in enumerate(s[::-1]):
    if len(suffix)==0:
        suffix += c
    else:
        if suffix[i-1]>c:
            break
        else:
            suffix += c

c = 'z'
for i in suffix:
    if i>s[25-len(suffix)]:
        c = min(c,i)

print(s[:25-len(suffix)]+c)

Submission status: Accepted

Solution code:
s = input()
t = list(s)

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

r = []
for c in t:
    r.append(ord(c))

if len(t) < 26:
    r = sorted(r)

    temp = ord('a')

    for x in r:
        if x == temp:
            temp += 1
        else:
            break

    t.append(chr(temp))

    print(''.join(t))

else:
    temp = r[25]
    for i in range(24, -1, -1):
        if r[i] < temp:
            hoge = r[i]
            fuga = sorted(r[i + 1:])
            for x in fuga:
                if x > hoge:
                    hoge = x
                    break
            t[i] = chr(hoge)
            break
        else:
            temp = max(temp, r[i])
            t[i] = ''

    t[25] = ''
    print(''.join(t))

Submission status: Accepted

Solution code:
s = input()
a = [chr(i) for i in range(ord('a'), ord('z')+1)]
if len(s)< 26:
    for i in a:
        if sorted(s).count(i)==0:
            break
    print(s+i)
else:
    l = [ord(i) for i in s]
    for i in range(1,len(s)):
        if l[-i]-l[-i-1]>0:
            k = sorted(s[-i:])
            for j in k:
                if ord(j)>ord(s[-i-1]):
                    break
            print(s[:-i-1]+j)
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
def a_diverse_word(S):
    from string import ascii_lowercase
    if len(S) < 26:
        # 26字未満なら、Sにまだ現れていない文字で辞書順最小のものを末尾に加えたものが解
        ans = S + min(set(list(ascii_lowercase)) - set(S))
    else:  # Sの長さは定義上26字以下なので、Sが26字のときこの処理を行う
        idx = 0  # S[idx]以降末尾まで降順になっている
        for k in range(len(S) - 1, 0, -1):
            if S[k] > S[k - 1]:
                idx = k
                break
        if idx == 0:
            ans = '-1'  # Sは辞書の最後の単語だった。「次」はない
        else:
            for c in sorted(S[idx:]):
                if S[idx - 1] < c:
                    ans = S[:idx - 1] + c
                    break
    return ans

S = input().strip()
print(a_diverse_word(S))

Submission status: Accepted

Solution code:
def a_diverse_word(S):
    from string import ascii_lowercase
    if len(S) < 26:
        # 26字未満なら、Sにまだ現れていない文字で辞書順最小のものを末尾に加えたものが解
        ans = S + min(set(list(ascii_lowercase)) - set(S))
    else:  # Sの長さは定義上26字以下なので、Sが26字のときこの処理を行う
        idx = 0  # S[idx]以降末尾まで降順になっている
        for k in range(len(S) - 1, 0, -1):
            if S[k] > S[k - 1]:
                idx = k
                break
        if idx == 0:
            ans = '-1'  # Sは辞書の最後の単語だった。「次」はない
        else:
            for c in ascii_lowercase:
                if (S[idx - 1] < c) and (c not in S[:idx]):
                    ans = S[:idx - 1] + c
                    break
    return ans

S = input().strip()
print(a_diverse_word(S))

Submission status: Accepted

Solution code:
s = input()

s_set = set(s)
a_set = set('abcdefghijklmnopqrstuvwxyz')
left_list = sorted(list(a_set - s_set))

ans = -1
if len(left_list) == 0:#未使用文字なし
    while ans == -1:
        left_list.append(s[-1])
        s = s[:-1]
        if len(s) == 0:
            break
        left_list = sorted(left_list)
        #print(s, left_list)
        for c in left_list:
            #print(c, s[-1])
            if c > s[-1]:
                ans = s[:-1] + c
                break
else:#未使用文字あり
    ans = s + left_list[0]
print(ans)