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()
for i in range(ord('a'),ord('z')+1):
  if chr(i) not in S:
    print(S+chr(i))
    break
else:
  for i in range(len(S)-1,-1,-1):
    for j in range(ord('a'),ord('z')+1):
      if j>ord(S[i]) and chr(j) not in S[:i]:
        print(S[:i]+chr(j))
        exit(0)
  else:
    print(-1)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 2019-11-27 15:57:35(JST)
import sys
from string import ascii_lowercase as alphabet
from bisect import bisect_left as bi_l

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 2019-11-27 15:57:35(JST)
import sys
from string import ascii_lowercase as alphabet
from bisect import bisect_left as bi_l

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

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

import numpy as np

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

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


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

for i in reversed(range(len(S))):
    # なんか1文字つける
    for c in list(string.ascii_lowercase):
        if c not in S:
            print(S + c)
            exit()
    for j in range(ord(S[i]) + 1, ord('z') + 1):
        if chr(j) not in S[:i]:
            print(S[:i] + chr(j))
            exit()

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

Submission status: Accepted

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

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

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

print('-1')

Submission status: Accepted

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

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

    else:
        ans = -1
        for i in range(1, 26)[::-1]:
            if s[i-1] < s[i]:
                for j in range(i, 26)[::-1]:
                    if s[j] > s[i-1]:
                        ans = s[:i-1] + s[j]
                        break
                break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

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




if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
alpha=list("abcdefghijklmnopqrstuvwxyz")
alpha_dict={a:i for i,a in enumerate(alpha)}
alpha = set(alpha)

string = input()

can_use_str = alpha - set(list(string))

if string == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)

elif len(can_use_str)==0:
    k = 0
    for i in reversed(range(25)):
        if string[i] < string[i + 1]:
            k = i
            break
    last = sorted(string[k:])
    for i in last:
        if string[k] < i:
            print(string[:k] + i)
            break

else:
    string+=sorted(list(can_use_str))[0]
    print(string)

Submission status: Accepted

Solution code:
s=str(input())
#print(len(s))
p='abcdefghijklmnopqrstuvwxyz'
p=list(p)
if s=='zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif len(s)!=26:
    for i in range(26):
        if p[i] not in s:
            print(s+p[i])
            break
elif len(s)==26:
    for i in range(24,-1,-1):
        if s[i]<s[i+1]:
            z=list(s[i+1:])
            z.sort()
            for j in range(len(z)):
                if s[i]<z[j]:
                    print(s[:i]+z[j])
                    break
            else:
                continue
            break

Submission status: Accepted

Solution code:
S = input()
character = list('abcdefghijklmnopqrstuvwxyz')

if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif len(S) < 26:
    for i in character:
        if i not in S:
            print(S + i)
            exit()
else:
    k = 0
    for i in reversed(range(25)):
        if S[i] < S[i + 1]:
            k = i
            break
    last = sorted(S[k:])
    for i in last:
        if S[k] < i:
            print(S[:k] + i)
            exit()

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(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:
alp = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s) < 26:
  for x in alp:
    if x not in s:
      print(s+x)
      break
elif s == alp[::-1]:
  print(-1)
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:
from collections import defaultdict
S = input()
alphabet = "abcdefghijklmnopqrstuvxyz"

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

dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(S) < len(alphabet):
    for a in alphabet:
        if dict[a] == 0:
            print(S + a)
            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()
print(-1)

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

s = input()

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

L = 26
s = [ord(c) - ord('a') for c in s]

memo = [False] * L
for c in s:
    memo[c] = True

lowerbound = 0
while all(memo[lowerbound:]):
    c = s.pop()
    lowerbound = c + 1
    memo[c] = False

for i in range(lowerbound, L):
    if not memo[i]:
        s.append(i)
        break

print(''.join(map(chr, [c + ord('a') for c in s])))

Submission status: Accepted

Solution code:
s = input()
atoz = "abcdefghijklmnopqrstuvwxyz"
if len(s) < 26:
  for i in atoz:
    if i not in s:
      print(s+i)
      break
else:
  for i in range(len(s)):
    si = s[i:]
    if si == "".join(sorted(si, reverse=True)):
      if i == 0:
        print(-1)
        exit()
      else:
        hoge = s[i - 1]
        moge = list(si)
        moge.sort()
        for j in moge:
          if j > hoge:
            print(s[:i-1]+j)
            exit()

Submission status: Accepted

Solution code:

import sys

S = input()
alp = "abcdefghijklmnopqrstuvwxyz"
alpnum = {}

for i in range(26):
    alpnum[alp[i]] = i

dic = {}

for i in S:

    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

if len(dic) < 26:

    for i in range(26):

        if alp[i] not in dic:
            print (S+alp[i])
            break

else:

    now = alpnum[S[-1]]
    lis = []
    for i in range(26):

        if now > alpnum[S[-1]]:

            lis.sort()
            ans = ""
            for j in lis:

                if alpnum[j] > alpnum[S[-1]]:
                    ans = j
                    break
                    
            print ( S[:-1] + ans )
            sys.exit()
        else:
            lis.append(S[-1])
            now = alpnum[S[-1]]
            S = S[:-1]

    
    print (-1)

Submission status: Accepted

Solution code:
def f(s):
    #print(len(s) != len(set(s)) or s[-1] >= 123)
    return len(s) != len(set(s)) or s[-1] >= 123


S = input()
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit(0)
S_lis = list(map(ord, S))
#print(S_lis)

S_lis += [97]
#print(S_lis)
while f(S_lis):
    if S_lis[-1] == 123:
        S_lis.pop()
    S_lis[-1] += 1
print(''.join(map(chr, S_lis)))

Submission status: Accepted

Solution code:
import string

A = [chr(i) for i in range(ord('a'), ord('z')+1)]
B = {}
for i in range(26):
    B[A[i]] = i

s = input()
n = len(s)


def a():
    C = [0] * 26
    for i in range(n):
        C[B[s[i]]] = 1

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

print(a())

Submission status: Accepted

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

if n<26:
    s=set()
    for c in S:
        s.add(c)
    for c in abc:
        if c not in s:
            ans=S+c
            break
else:
    l=[]
    i=25
    l.append(S[i])
    while i>0 and S[i-1]>l[-1]:
        l.append(S[i-1])
        i-=1
    if i==0:
        ans='-1'
    else:
        c=S[i-1]
        l.append(c)
        l.sort()
        for j in range(len(l)):
            if l[j]==c:
                d=l[j+1]
        ans=S[:i-1]+d 

print(ans)

Submission status: Accepted

Solution code:
def f(s):
    return len(s) != len(set(s)) or s[-1] >= 123


st = input()
if st == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
    exit(0)
s = list(map(ord, st))
s += [97]
while f(s):
    if s[-1] == 123:
        s.pop()
    s[-1] += 1
print(''.join(map(chr, s)))

Submission status: Accepted

Solution code:
s=input()
if len(s)==26:
	d=[]
	for i in range(26):
		if list(s[-i-1:][::-1])!=sorted(s[-i-1:]):
			for j in range(i+1):
				d.append(s[-j-1])
			d.sort()
			for j in range(len(d)):
				if d[j]>s[-i-1]:
					k=d[j]
					break
			print(s[:-i-1]+k)
			exit()
	print(-1)
	exit()
d=[0]*26
for i in range(len(s)):
	d[ord(s[i])-ord("a")]+=1
for i in range(26):
	if d[i]==0:
		x=chr(ord("a")+i)
		break
print(s+x)

Submission status: Accepted

Solution code:
s=input()
if len(s)==26:
	d=[]
	for i in range(26):
		if list(s[-i-1:][::-1])!=sorted(s[-i-1:]):
			for j in range(i+1):
				d.append(s[-j-1])
			d.sort()
			if d[0]==s[-i-1]:
				k=d[1]
			else:
				for j in range(len(d)):
					if d[j]>s[-i-1]:
						k=d[j]
						break
			print(s[:-i-1]+k)
			exit()
	print(-1)
	exit()
d=[0]*26
for i in range(len(s)):
	d[ord(s[i])-ord("a")]+=1
for i in range(26):
	if d[i]==0:
		x=chr(ord("a")+i)
		break
print(s+x)

Submission status: Accepted

Solution code:
import string
s = input()
lower = list(string.ascii_lowercase)

if len(s) < 26:
    for c in s: lower.remove(c)
    print(s + lower[0])
else:
    for i in range(24, -1, -1):
        if list(s[i:]) != sorted(s[i:])[::-1]:
            ans = s[:i]
            for j in range(1, 25):
                last = lower[lower.index(s[i])+j]
                if not last in ans:
                    ans += last
                    break
            print(ans)
            break
    else: print(-1)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# a〜z
l=['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']

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

  for x in s:
    if l.count(x)==0:
      return -1
    l.remove(x)

  if len(l)>0:
    return ''.join(s)+l[0]

  nakama=[]
  for i in range(len(s))[::-1]:
    nakama.sort()
    b=s[i]
    for a in nakama:
      if a>b:
        return s[:i]+a
    nakama.append(b)

# main()
print(main())

Submission status: Accepted

Solution code:
import sys
S = input()
if len(S) < 26:
    for i in range(97, 97 + 26):
        if chr(i) not in S:
            print(S+chr(i))
            break
else:
    S_rev = S[::-1]
    for i in range(len(S_rev)-1):
        if S_rev[i] > S_rev[i+1]:
            for j in range(len(S_rev)):
                if S_rev[j] > S[-(i+2)]:
                    print(S[:len(S)-(i+1)-1]+S_rev[j])
                    sys.exit(0)
    print(-1)

Submission status: Accepted

Solution code:
S=input()
for c in"abcdefghijklmnopqrstuvwxyz":
 if(c in S)^1:print(S+c);quit()
for i in range(1,27):
 for c in sorted(S[-i:]):
  if c>S[-i]:print(S[:-i]+c);quit()
print(-1)

Submission status: Accepted

Solution code:
S=input()
N=len(S)
A="abcdefghijklmnopqrstuvwxyz"

if N<26:
    for a in A:
      if a not in S:
        S+=a
        break
else:
  if S==A[::-1]:
    S="-1"
  else:
    for i in range(N-1,-1,-1):
      s=S[i:]
      ss=list(S[i:])
      ss.sort(reverse=True)
      ss="".join(ss)
      #print(i,s,ss)
      if s!=ss:
        S=S[:i]+ss[ss.index(S[i])-1]
        break
print(S)

Submission status: Accepted

Solution code:
import string

S = input()
V = set(S)

for s in string.ascii_lowercase:
    if not s in V:
        print(S + s)
        exit()

for s, t in zip(S[:: -1], string.ascii_lowercase):
    if s != t:
        break
else:
    print(-1)
    exit()

for i in range(len(S))[:: -1]:
    A = []
    for j in range(i + 1, len(S)):
        if S[i] < S[j]:
            A.append(S[j])
    if A:
        A.sort()
        print(S[: i] + A[0])
        break

Submission status: Accepted

Solution code:
s = input()
n = len(s)
alphabet = [chr(ord("a")+i) for i in range(26)]
if n != 26:
    for i in alphabet:
        if i not in s:
            s += i
            break
    print(s)
else:
    for i in range(n-1, -1, -1):
        t = s[:i]
        n = alphabet.index(s[i])
        for j in range(n+1, 26):
            if alphabet[j] not in t:
                print(t+alphabet[j])
                exit()
    print(-1)

Submission status: Accepted

Solution code:
s=input()
N=len(s)
alpha="abcdefghijklmnopqrstuvwxyz"
a=[0]*26
for i in range(N):
    for j in range(26):
        if s[i]==alpha[j]:
            a[j]+=1
            break

if s=="zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
c=0
lastidx=-1
while True:
    for j in range(lastidx+1,26):
        if a[j]==0 :
            print(s[:N+c]+alpha[j])
            exit()
    lastidx=alpha.index(s[-1+c])
    a[lastidx] -= 1
    c-=1

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    if len(S) < 26:
        used = [0]*26
        for c in S:
            used[ord(c)-ord("a")] = 1
        print(S+chr(ord("a")+used.index(0)))
        return
    used = []
    while len(S)>0:
        c = S[-1]
        S = S[:-1]
        nxt = []
        for c2 in used:
            if c < c2:
                nxt.append(c2)
        if nxt:
            print(S+min(nxt))
            return
        used.append(c)
    print("-1")

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
s=input()
al=set(chr(ord('a')+i) for i in range(26))

def f(s,al):
    if len(s)==0:
        return -1
    t=f(s[1:],al-set([s[0]]))
    if t==(-1):
        larger=[x for x in al if x>s[0]]
        if not larger:
            return -1
        return min(larger)
    return s[0]+t

ans=f(s,al)
print(ans)

Submission status: Accepted

Solution code:
def main():
    S = str(input())
    sl =  list(S)
    if len(S) != 26:
        alpha = [i for i in range(26)]
        for i in sl:
            alpha.remove(ord(i)-97)
        n = min(alpha)
        print(S+chr(97+n))
    elif S == "zyxwvutsrqponmlkjihgfedcba":
        print(-1)
    else:
        alpha = []
        for i in range(25):
            alpha.append(S[-i-1])
            if ord(max(alpha)) > ord(S[-i-2]):
                alpha.sort()
                for j in alpha:
                    if ord(j) > ord(S[-i-2]):
                        print(S[:(-i-2)]+j)
                        exit()

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect

S = input()

N = len(S)
if S == 'zyxwvutsrqponmlkjihgfedcba':
    print(-1)
elif N < 26:
    t = [False] * 26
    for i in range(N):
        t[ord(S[i]) - 97] = True
    print(S + chr(t.index(False) + 97))
else:
    t = [S[-1]]
    for i in range(N - 2, -1, -1):
        if S[i] < S[i + 1]:
            break
        bisect.insort_left(t, S[i])
    print(S[:i] + t[bisect.bisect_left(t, S[i])])

Submission status: Accepted

Solution code:
S = input()

abc = list("abcdefghijklmnopqrstuvwxyz")
# ord('a') = 97
n = len(abc)
check = [0] * n
for s in S:
    check[ord(s) - 97] = 1

if S == ''.join(abc[::-1]):
    print(-1)
    exit()

if sum(check) != n:
    for i in range(n):
        if check[i] == 0:
            S += abc[i]
            break
    print(S)
else:
    check = [0] * n
    for i in range(0, n):
        check[ord(S[::-1][i]) - 97] = 1
        if S[::-1][i] > S[::-1][i + 1]:
            c = 24 - i
            break
    # print(c)
    # print(check)
    # print(S[c])
    ans = S[:c]
    for i in range(ord(S[c]) - 97, n):
        if check[i] == 1:
            ans += abc[i]
            break
    print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)

D = [0 for i in range(26)]
for i in range(N):
    D[ord(S[i]) - 97] = 1

if N != 26:
    for i in range(26):
        if D[i] == 0:
            s = chr(97 + i)
            break
    print(S + s)

else:
    if S == "abcdefghijklmnopqrstuvwxyz"[::-1]:
        print(-1)
    else:
        c = 2
        for i in range(N):
            if ord(S[-2 - i]) - ord(S[-1 - i]) >= 1:
                c += 1
            else:
                break
        s = sorted(list(S[26 - c:]))
        for i in s:
            if ord(i) > ord(S[-c]):
                key = i
                break

        print(S[:26 - c] + key)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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 LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = list(input())
    n = len(s)
    alp = list("abcdefghijklmnopqrstuvwxyz")
    f = {}
    for i in range(26):
        f[alp[i]] = i
    if n == 26:
        if s == alp[::-1]:
            print(-1)
        else:
            d = [1]*26
            m = f[s[-1]]
            d[m] = 0
            for i in range(n-1)[::-1]:
                if m > f[s[i]]:
                    break
                m = f[s[i]]
                d[m] = 0
            k = f[s[i]]
            for j in range(k+1,n):
                if not d[j]:
                    break
            s = s[:i]+[alp[j]]
            print(*s,sep = "")
    else:
        d = [1]*26
        for i in s:
            d[f[i]] = 0
        for i in range(26):
            if d[i]:
                break
        s += [alp[i]]
        print(*s,sep = "")
    return
#B
def B():

    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

Solution code:
s = input()
alphabet_small = [chr(i) for i in range(97, 97+26)]
avail = [True for i in range(26)]
for i in s:
    for j in range(26):
        if alphabet_small[j] == i:
            avail[j] = False
            break

for i in range(26):
    if avail[i]:
        print(s+alphabet_small[i])
        exit()

for i in range(25,-1,-1):
    alpha = s[i]
    k = -1
    for j in range(26):
        if alphabet_small[j] == alpha:
            avail[j] = True
            k = j
            break
    for j in range(26):
        if j > k and avail[j]:
            print(s[:i]+alphabet_small[j])
            exit()

print(-1)

Submission status: Accepted

Solution code:
def main():
    S = input()
    alpha = "abcdefghijklmnopqrstuvwxyz"
    alpha_rev = alpha[::-1]
    if S == alpha_rev:
        return -1
    Sset = set(list(S))
    if len(Sset) != 26:
        for c in alpha:
            if c not in Sset:
                return S + c
    else:
        for i in range(len(S)-1, 0, -1):
            if S[i] > S[i-1]:
                target = sorted(S[i-1:])
                for j in range(len(target)):
                    if target[j] > S[i-1]:
                        ref = target[j]
                        break
                t = S[:i-1] + ref
                return t
        return -1

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
from collections import defaultdict
S = input()
alphabet = "abcdefghijklmnopqrstuvxyz"

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

dict = defaultdict(int)
for s in S:
    dict[s] += 1

if len(S) < len(alphabet):
    for a in alphabet:
        if dict[a] == 0:
            print(S + a)
            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()
print(-1)