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

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


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

def main():
	S = input().rstrip()
	# zyxwvutsrqponmlkjihgfedcba の例
	if S == alp[::-1]:
		print(-1)
		sys.exit()
	# 使われていない文字が残っている場合
	for i, letter in enumerate(alp):
		if not S.count(letter):
			print(S+letter)
			sys.exit()
	# 26文字全て使われている場合
	# => ABCDE(01234) -> ABCE(0124) にする
	tmp = []
	for i in range(len(S)-1, 0, -1):
		if S[i] < S[i-1]:
			tmp.append(S[i])
		elif S[i] > S[i-1]:
			tmp.append(S[i])
			tmp.sort()
			print(S[:i-1]+tmp[0])
			sys.exit()

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

def main():
	S = input().rstrip()
	for i, letter in enumerate(alp):
		if not S.count(letter):
			print(S+letter)
			sys.exit()
	for i in range(len(alp)-1):
		if S[i+1] == 'z':
			print(S[:i]+alp[i+1])
			sys.exit()
			
	print(-1)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
s = input()
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
al = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
al_l = [chr(ord('a') + i) for i in range(26)]
d,d_al = {},{}
for i in range(26):
    d[chr(ord('a') + i)] = i+1
for i in range(26):
    d_al[i+1] = chr(ord('a') + i)

ss = sorted(list(set(al_l) - (set(s))))
if len(s) <= 25:
    print(s+ss[0])
    exit()
    
L = []
for i in range(len(s)):
    L.append(d[s[i]])

ans = []
for i in range(1,len(L)):
    if L[i] == al[i]:
        ans.append(L[i-1])
    elif L[i] != al[i]:
        ans.append(min(L[i:]))
        break

s_ans = ""
for i in range(len(ans)):
    s_ans += d_al[ans[i]]
print(s_ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
S = input()
l = "abcdefghijklmnopqrstuvwxyz"
if len(S) < 26:
    for i in range(len(l)):
        if l[i] not in S:
            print(S + l[i])
            exit()
else:
    s = S[-1]
    for i in range(len(S)-1)[::-1]:
        if s < S[i]:
            print(S[:i-1] + s)
            exit()
        else:
            s = S[i]
print(-1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
S = input()
l = "abcdefghijklmnopqrstuvwxyz"
if len(S) < 26:
    for i in range(len(l)):
        if l[i] not in S:
            print(S + l[i])
            exit()
else:
    s = S[-1]
    for i in range(len(S)-1)[::-1]:
        if s < S[i]:
            print(S[:i-1] + s)
            exit()
        else:
            s = S[i]

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
al = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    k = []
    for i in range(25, 0, -1):
      if s[i-1] < s[i]:
        for j in sorted(k):
          if j > s[i-1]:
            print(s[:i-1] + j)
            exit()
      k.append(s[i])

Submission status: Wrong Answer

Solution code:
al = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    k = set()
    for i in range(25, 0, -1):
      if s[i-1] < s[i]:
        for j in k:
          if j > s[i-1]:
            print(s[:i-1] + j)
            exit()
      k.add(s[i])

Submission status: Wrong Answer

Solution code:
al = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    k = chr(ord("z")+1)
    for i in range(25, 0, -1):
      k = min(k, s[i])
      if s[i-1] < s[i]:
        print(s[:i-1] + k)
        break

Submission status: Wrong Answer

Solution code:
al = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    k = chr(ord("z")+1)
    for i in range(25, 0, -1):
      if s[i-1] < s[i]:
        print(s[:i-1] + k)
        break
      k = min(k, s[i])

Submission status: Wrong Answer

Solution code:
al = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
  else:
    k = chr(ord("a")-1)
    for i in range(25, 0, -1):
      if s[i-1] < s[i]:
        print(s[:i-1] + k)
        break
      k = max(k, s[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
S = input()

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

if any(can_use):
    for n, use in enumerate(can_use):
        if use:
            S += chr(ord('a') + n)
            print(S)
            exit()
else:
    # 全て使われている
    for i in range(len(S)-1, 1, -1):
        if ord(S[i-1]) < ord(S[i]):
            c = sorted(S[i:])[0]
            ans = S[:i-1] + c
            print(ans)
            exit()

    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

        res = ""
        inv_s = s[::-1]
        _max = -1
        for i, j in enumerate(inv_s):
            if _max > ls[j]:
                break
            _max = max(_max, ls[j])
        res = s[:-1-i]
        candidate = list(set(x).difference(set(res + inv_s[i])))
        candidate.sort()
        print(res + candidate[0])

Submission status: Wrong Answer

Solution code:
s = input()
len_s = len(s)
x = "abcdefghijklmnopqrstuvwxyz"
dic = {x[i]: x[i+1] for i in range(25)}
if len_s < 26:
    set_s = set(s)
    candidate = list(set(x).difference(set_s))
    candidate.sort()
    print(s + candidate[0])
else:
    if s == x[::-1]:
        print(-1)
    else:
        res = ""
        for i in range(len_s):
            if s[i] == "z":
                res = s[:i-1]
                break
        candidate = list(set(x).difference(set(res + s[i-1])))
        candidate.sort()
        print(res + candidate[0])

Submission status: Wrong Answer

Solution code:
S = input()
# 接尾辞の降順が崩れるタイミングが狙い目
if len(S) == 26:
    # 降順が崩れるタイミングを狙う
    pointa = False
    for i in reversed(range(1, 26)):
        if ord(S[i]) > ord(S[i-1]):  # 後ろから見て降順が崩れたタイミング
            pointa = i-1
            break
    # 後ろからi-1文字目を書き換えればok
    if pointa:
        happend = list(S[:pointa])  # ここに出ていないかつ, S[pointa]より大きい最小のもの
        for i in range(26):
            if chr(ord("a") + i) not in happend and ord("a")+i > ord(S[pointa]):
                target = chr(ord("a") + i)
                break
        print(S[:pointa]+target)
    else:
        print(-1)

else:
    # 1文字付け足すだけ
    happend = list(S)
    for i in range(26):
        if chr(ord("a") + i) not in happend:
            target = chr(ord("a") + i)
            break
    print(S+target)

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if len(S) == 26:
    now = S[-1]
    for i in reversed(range(1, 26)):
        if ord(now)+1 != ord(S[i]):  # 辞書順が崩れた瞬間
            happened = list(S[:i])
            for j in range(26):
                if chr(ord("a") + j) not in happened:
                    print(S[:i]+chr(ord("a") + j))
                    exit()
        else:
            now = S[i]

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

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if len(S) == 26:
    now = S[-1]
    for i in reversed(range(1, 26)):
        if ord(now)+1 != ord(S[i]):  # 辞書順が崩れた瞬間
            happened = list(S[:i+1])
            for j in range(26):
                if chr(ord("a") + j) not in happened:
                    print(S[:i]+chr(ord("a") + j))
                    exit()
        else:
            now = S[i]

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

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if len(S) == 26:
    now = S[-1]
    for i in reversed(range(1, 26)):
        if ord(now)+1 != ord(S[i]):  # 辞書順が崩れた瞬間
            happened = list(S[:i+1])
            for j in range(26):
                if chr(ord("a") + j) not in happened and ord("a") + j > ord(S[i]):
                    print(S[:i]+chr(ord("a") + j))
                    exit()
        else:
            now = S[i]

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

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if len(S) == 26:
    now = S[-1]
    for i in reversed(range(1, 26)):
        if ord(now)+1 != ord(S[i]):  # 辞書順が崩れた瞬間
            happened = list(S[:i])
            for j in range(26):
                if chr(ord("a") + j) not in happened and ord("a") + j > ord(S[i]):
                    print(S[:i]+chr(ord("a") + j))
                    exit()
        else:
            now = S[i]

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

Submission status: Wrong Answer

Solution code:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if len(S) == 26:
    now = S[-1]
    for i in reversed(range(1, 26)):
        if ord(now) > ord(S[i]):  # 辞書順が崩れた瞬間
            happened = list(S[:i])
            for j in range(26):
                if chr(ord("a") + j) not in happened and ord("a") + j > ord(S[i]):
                    print(S[:i]+chr(ord("a") + j))
                    exit()
        else:
            now = S[i]

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

Submission status: Wrong Answer

Solution code:
# アルファベットが全て異なっているならば多彩である
class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        # ステップ1：kを求める
        k = -1
        i = len(nums) - 2
        while i >= 0:
            if nums[i] < nums[i + 1]:
                k = i
                break
            i -= 1

        # kがなかったら、配列は逆順にソートされているので、反転して終了
        if k == -1:
            nums.reverse()
            return

        # ステップ2：lを求め、nums[k]とnums[l]を入れ替える
        l = k + 1
        i = len(nums) - 1
        while i > k + 1:
            if nums[k] < nums[i]:
                l = i
                break
            i -= 1

        tmp = nums[k]
        nums[k] = nums[l]
        nums[l] = tmp

        # ステップ3：nums[k]より右側の要素を反転させる
        nums[k+1:] = reversed(nums[k+1:])


S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
    exit()
if len(S) == 26:
    now = S[-1]
    for i in reversed(range(1, 26)):
        if ord(now) > ord(S[i]):  # 辞書順が崩れた瞬間
            happened = list(S[:i])
            for j in range(26):
                if chr(ord("a") + j) not in happened and ord("a") + j > ord(S[i]):
                    print(S[:i]+chr(ord("a") + j))
                    exit()

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

Submission status: Wrong Answer

Solution code:
# アルファベットが全て異なっているならば多彩である
S = input()
if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba" or S == "abcdefghijklmnopqrstuvwxyz":
        print(-1)
        exit()
    # コーナーケースの処理
    # 限界まで同じ文字をつらぬく
    # 昇順になっていないところにぶち込む
    now = S[0]
    ans = S[0]
    for i in range(1, len(S)):
        if ord(now) + 1 != ord(S[i]):  # つけいるスキがあるとき
            happened = list(ans)
            for i in range(26):
                if chr(ord("a") + i) not in happened:
                    print(ans[0:len(ans)-1]+chr(ord("a") + i))
                    exit()
        else:
            ans += S[i]
            now = S[i]
    print(-1)
else:
    happened = list(S)
    for i in range(26):
        if chr(ord("a") + i) not in happened:
            print(S+chr(ord("a") + i))
            exit()

Submission status: Wrong Answer

Solution code:
import sys
import bisect

stdin = sys.stdin

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

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

Submission status: Wrong Answer

Solution code:
s = list(input())

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

Submission status: Wrong Answer

Solution code:
s = list(input())

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

Submission status: Wrong Answer

Solution code:
al = "abcdefghijklmnopqrstuvwxyz"
s = input()
if len(s)<26:
  for i in al:
    if i not in s:
      print(s+i)
      break
else:
  d = { i:j for j, i in enumerate(al)}
  for i in range(25, -1, -1):
    k = d[s[i]]
    if k == 25:
      continue
    if al[k+1] not in s[:i]:
      print(s[:i]+al[k+1])
      exit()
  print(-1)

Submission status: Wrong Answer

Solution code:
s = list(input())

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
import string

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

for i in s:
    l.remove(i)

if s == string.ascii_lowercase[::-1]:
    print(-1)
    exit()
elif len(l) > 0:
    print(s+l[0])
else:
    li = []
    k = s[::-1]
    for i in range(len(k)):
        li.append(k[i])
        if sorted(li) != li:
            li.pop()
            print(s[:-1*(len(li)+1)]+sorted(li)[0])
            break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = input()
L = [i for i in range(97,123)]
n = len(S)
for i in range(n):
    L.remove(ord(S[i]))
if n <= 25:
    X = L[0]
    ans = S + chr(X)
else:
    T = []
    p = 0
    q,r = -1,-1
    for i in range(n):
        j = S[i]
        if ord(j) > p:
            q = i
            T = []
        else:
            if len(T) == 0:
                r = i
                T.append(S[i-1])
            T.append(j)
        p = ord(j)
    if len(T) == 0:
        T.append(S[n-1])
        r = n
    if r == 1:
        ans = -1
    else:
        ans = S[0]
        for i in range(1,r-2):
            ans += S[i]
        for i in range(1,len(T)+1):
            if S[r-2] < T[-i]:
                ans += T[-i]
                break
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
L = [i for i in range(97,123)]
n = len(S)
for i in range(n):
    L.remove(ord(S[i]))
if n <= 25:
    X = L[0]
    ans = S + chr(X)
else:
    T = []
    p = 0
    q,r = -1,-1
    for i in range(n):
        j = S[i]
        if ord(j) > p:
            q = i
            T = []
        else:
            if len(T) == 0:
                r = i
                T.append(S[i-1])
            T.append(j)
        p = ord(j)
    if len(T) == 0:
        T.append(S[n-1])
        r = n
    if (r == 1) & (S[r-1] == 'z'):
        ans = -1
    else:
        ans = S[0]
        for i in range(1,r-2):
            ans += S[i]
        for i in range(1,len(T)+1):
            if S[r-2] < T[-i]:
                ans += T[-i]
                break
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
L = [i for i in range(97,123)]
n = len(S)
for i in range(n):
    L.remove(ord(S[i]))
if n <= 25:
    X = min(L)
    ans = S + chr(X)
else:
    T = []
    p = 0
    q,r = -1,-1
    for i in range(n):
        j = S[i]
        if ord(j) > p:
            q = i
            T = []
        else:
            if len(T) == 0:
                r = i
                T.append(S[i-1])
            T.append(j)
        p = ord(j)
    if q < r:
        if (r == 1) & (S[r-1] == 'z'):
            ans = -1
        else:
            ans = S[0]
            for i in range(1,r-2):
                 ans += S[i]
            for i in range(1,len(T)+1):
                if S[r-2] < T[-i]:
                    ans += T[-i]
                    break
    else:
        ans = S[0]
        for i in range(1,n-2):
            ans += S[i]
        ans += S[n-1]
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
L = [i for i in range(97,123)]
n = len(S)
for i in range(n):
    L.remove(ord(S[i]))
if n <= 25:
    X = L[0]
    ans = S + chr(X)
else:
    T = []
    p = 0
    q,r = -1,-1
    for i in range(n):
        j = S[i]
        if ord(j) > p:
            q = i
            T = []
        else:
            if len(T) == 0:
                r = i
                T.append(S[i-1])
            T.append(j)
        p = ord(j)
    if q < r:
        if (r == 1) & (S[r-1] == 'z'):
            ans = -1
        else:
            ans = S[0]
            for i in range(1,r-2):
                 ans += S[i]
            for i in range(1,len(T)+1):
                if S[r-2] < T[-i]:
                    ans += T[-i]
                    break
    else:
        ans = S[0]
        for i in range(1,n-2):
            ans += S[i]
        ans += S[n-1]
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
L = [i for i in range(97,123)]
n = len(S)
for i in range(n):
    L.remove(ord(S[i]))
if n <= 25:
    X = L[0]
    ans = S + chr(X)
else:
    T = []
    p = 0
    q,r = -1,-1
    for i in range(n):
        j = S[i]
        if ord(j) > p:
            q = i
            T = []
        else:
            if len(T) == 0:
                r = i
            T.append(j)
        p = ord(j)
    if q < r:
        if (r == 1) & (S[r-1] == 'z'):
            ans = -1
        else:
            ans = S[0]
            for i in range(1,r-2):
                 ans += S[i]
            for i in range(1,len(T)+1):
                if S[r-2] < T[-i]:
                    ans += T[-i]
                    break
    else:
        ans = S[0]
        for i in range(1,n-2):
            ans += S[i]
        ans += S[n-1]
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
L = [i for i in range(97,123)]
n = len(S)
for i in range(n):
    L.remove(ord(S[i]))
if n <= 25:
    X = L[0]
    ans = S + chr(X)
else:
    T = []
    p = 0
    q,r = -1,-1
    for i in range(n):
        j = S[i]
        if ord(j) > p:
            q = i
            T = []
        else:
            if len(T) == 0:
                r = i
            T.append(j)
        p = ord(j)
    if q < r:
        if (r == 1) & (S[r-1] == 'z'):
            ans = -1
        else:
            ans = S[0]
            for i in range(1,r-2):
                 ans += S[i]
            ans += T[-1]
    else:
        ans = S[0]
        for i in range(1,n-2):
            ans += S[i]
        ans += S[n-1]
print(ans)

Submission status: Wrong Answer

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

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

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

    print(s[:j] + min(s[j + 1:]))

Submission status: Wrong Answer

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

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

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

    print(s[:j] + min(s[j + 1:]))

Submission status: Wrong Answer

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

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

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

    print(s[:j] + min(s[j + 1:]))

Submission status: Wrong Answer

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

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

if len(s) != 26:
    print(s + sorted(list(set(a) - set(s)))[0])
else:
    ans = ""
    for i in range(len(s) - 1):
        if s[i] > s[i + 1]:
            break
        ans += s[i]

    print(ans[:-1] + min(s[i:]))

Submission status: Wrong Answer

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

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

if len(s) != 26:
    print(s + sorted(list(set(a) - set(s)))[0])
else:
    ans = ""
    for i in range(26):
        if a[i] == s[i]:
            ans += a[i]
        else:
            print(ans[:-1] + a[i])
            exit()

Submission status: Wrong Answer

Solution code:
from string import ascii_lowercase
S = input()
n = len(S)

if n != 26:
    ans = S
    for c in ascii_lowercase:
        if c not in S:
            ans += c
            break
else:
    pos = [0] * 26
    for i in range(26):
        for j in range(26):
            if S[i] == ascii_lowercase[j]:
                pos[j] = i
    # print(pos)
    ss = [0] * 26
    for i in range(26):
        for j in range(26):
            if S[j] == ascii_lowercase[i]:
                ss[j] = i
    # print(ss)

    mx = 0
    lst = []
    for i in range(26):
        if S[25-i] != ascii_lowercase[i]:
            if ss[25-i] > mx:
                mx = max(mx, ss[25-i])
                lst.append(S[25-i])
            else:
                break
    if i == 25:
        ans = -1
    else:
        lst.sort()
        # print(lst)
        for j in lst:
            if j > S[25-i]:
                ans = S[:25-i] + j
                # print(i, j, pos[25-i])
                break

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
from string import ascii_lowercase
S = input()
n = len(S)

if n != 26:
    ans = S
    for c in ascii_lowercase:
        if c not in S:
            ans += c
            break
else:
    pos = [0] * 26
    for i in range(26):
        for j in range(26):
            if S[i] == ascii_lowercase[j]:
                pos[j] = i

    mx = 0
    for i in range(26):
        if S[25-i] != ascii_lowercase[i]:
            if pos[25-i] > mx:
                mx = max(mx, pos[25-i])
            else:
                break
    if i == 25:
        ans = -1
    else:
        ans = S[:25-i] + S[25]

print(ans)