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: Runtime Error

Solution code:

#ifndef TEMPLATE_HPP
#define TEMPLATE_HPP

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define _GLIBCXX_DEBUG

// Utility
using std::declval;
using std::distance;
using std::enable_if_t;
using std::get;
using std::index_sequence;
using std::is_array;
using std::is_pointer;
using std::is_same;
using std::iterator_traits;
using std::make_index_sequence;
using std::make_pair;
using std::make_tuple;
using std::make_unique;
using std::random_access_iterator_tag;
using std::stold;
using std::stoll;
using std::operator""s;
using std::abs;

// Types
using std::bitset;
using std::deque;
using std::list;
using std::multiset;
using std::nullptr_t;
using std::pair;
using std::tuple;
using std::unordered_multimap;
using std::unordered_multiset;
using ll = /** __int128; //*/ long long int;
using lf = long double;
using str = std::string;
template <class T> using u_set = std::unordered_set<T>;
template <class T> using vec = std::vector<T>;
using vecl = vec<ll>;
using vecf = vec<lf>;
using vecs = vec<str>;
template <class K, class V> using u_map = std::unordered_map<K, V>;
template <class V> using graph = u_map<V, u_set<V>>;
template <typename _Signature> using fun = std::function<_Signature>;

// Algorithms
using std::accumulate;
using std::all_of;
using std::any_of;
using std::back_inserter;
using std::copy;
using std::copy_if;
using std::count;
using std::count_if;
using std::find_if;
using std::for_each;
using std::inserter;
using std::lower_bound;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::next_permutation;
using std::none_of;
using std::partial_sum;
using std::remove_if;
using std::replace_if;
using std::reverse;
using std::set_difference;
using std::set_intersection;
using std::set_symmetric_difference;
using std::set_union;
using std::sort;
using std::swap;
using std::transform;
using std::unique;
using std::upper_bound;

namespace utils {
str to_string(__int128 const &x) { return std::to_string((long long)x); }
template <class T> T clamp(T const &v, T const &l, T const &h) {
  return min(h, max(l, v));
}
ll gcd(ll p, ll q) { return (q == 0) ? p : gcd(q, p % q); }
ll lcm(ll p, ll q) { return p / gcd(q, p) * q; }
} // namespace utils
using std::to_string;
using utils::clamp;
using utils::gcd;
using utils::lcm;
using utils::to_string;

namespace io {
// Input
using IS = std::istream;
IS &in(IS &);
IS &in(IS &i) { return i; }
template <class T, class... Ts> IS &in(IS &i, T &a, Ts &... as) {
  return in(i >> a, as...);
}
IS &operator>>(IS &i, __int128 &x) {
  long long int xx;
  i >> xx;
  x = xx;
  return i;
}
template <class V> IS &operator>>(IS &i, vec<V> &a) {
  for (auto &ai : a)
    i >> ai;
  return i;
}
template <class F, class S> IS &operator>>(IS &i, pair<F, S> &p) {
  return i >> p.first >> p.second;
}
template <class... Ts> IS &operator>>(IS &i, tuple<Ts...> &t) {
  return apply([&](auto &... ts) -> IS & { return in(i, ts...); }, t);
}
// Output
using OS = std::ostream;
OS &out_join(OS &o, str const &) { return o; }
template <class T> OS &out_join(OS &o, str const &, T const &a) {
  return o << a;
}
template <class T, class... Ts>
OS &out_join(OS &o, str const &sep, T const &a, Ts const &... as) {
  return out_join(o << a << sep, sep, as...);
}
OS &operator<<(OS &o, __int128 const &x) { return o << (long long int)x; }
template <class F, class S> OS &operator<<(OS &o, pair<F, S> const &p) {
  return o << p.first << ":" << p.second;
}
template <class... Ts, size_t... I>
OS &tuple_out(OS &o, tuple<Ts...> const &t, index_sequence<I...>) {
  return out_join(o, ",", get<I>(t)...);
}
template <class... Ts> OS &operator<<(OS &o, tuple<Ts...> const &t) {
  return tuple_out(o << "(", t, make_index_sequence<sizeof...(Ts)>()) << ")";
}
template <class T> struct Joiner {
  str const pre;
  str const post;
  str const sep;
  vec<T> const container;
  template <class Itr>
  Joiner(Itr const &begin, Itr const &end, str const &sep, str const &pre,
         str const &post)
      : pre(pre), post(post), sep(sep), container(begin, end) {}
};
template <class T> OS &operator<<(OS &o, Joiner<T> const &joiner) {
  auto itr = joiner.container.begin();
  auto end = joiner.container.end();
  o << joiner.pre;
  if (itr != end)
    o << *(itr++);
  while (itr != end)
    o << joiner.sep << *(itr++);
  return o << joiner.post;
}
template <class Itr>
auto join(Itr const &b, Itr const &e, str const &sep = ""s,
          str const &pre = ""s, str const &post = ""s) {
  using T = typename iterator_traits<Itr>::value_type;
  return Joiner<T>(b, e, sep, pre, post);
}
template <class C, enable_if_t<!is_same<C, str>::value && !is_array<C>::value &&
                                   !is_pointer<C>::value &&
                                   !std::is_arithmetic<C>::value,
                               nullptr_t> = nullptr>
OS &operator<<(OS &o, C const &a) {
  return o << join(a.begin(), a.end(), ",", "[", "]");
}
} // namespace io
using std::cerr;
using std::cin;
using std::cout;
auto init_io = []() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << std::fixed << std::setprecision(15);
};
auto input = [](auto &... a) { io::in(cin, a...); };
auto print = [](auto const &... a) { io::out_join(cout, " ", a...) << "\n"; };
#ifdef JUMPAKU_DEBUG
auto dump = [](auto const &... a) { io::out_join(cerr, " "s, a...) << "\n"; };
#else
auto dump = [](auto const &...) {};
#endif
using io::join;

// Hash
namespace hashcode {
template <class... Ts> size_t hash_args(size_t h, Ts const &... ts);
size_t hash_args(size_t h) { return h; }
template <class T, class... Ts>
size_t hash_args(size_t h, T const &t, Ts const &... ts) {
  constexpr std::hash<T> hasher;
  return hash_args(((h << 19) - h) ^ hasher(t), ts...);
}
template <class... Ts, size_t... I>
size_t hash_tuple(tuple<Ts...> const &t, index_sequence<I...>) {
  return hash_args(17, get<I>(t)...);
}
} // namespace hashcode
namespace std {
#if __GNUC__ == 5 || defined(__STRICT_ANSI__)
template <> struct hash<__int128> {
  size_t operator()(__int128 const &t) const {
    return (t & 0xffffff) ^ ((t >> 63) & 0xffffff);
  }
};
#endif
template <class... Ts> struct hash<tuple<Ts...>> {
  size_t operator()(tuple<Ts...> const &t) const {
    size_t h = hashcode::hash_tuple(t, index_sequence_for<Ts...>());
    return h ^ (h >> 32);
  }
};
template <class T> struct hash<u_set<T>> {
  size_t operator()(u_set<T> const &t) const {
    constexpr std::hash<T> hasher;
    return accumulate(
        t.begin(), t.end(), (size_t)0,
        [&](auto acc, auto const &ti) { return acc + hasher(ti); });
  }
};
} // namespace std

// Range
namespace ranges {
template <class T, class Itr> struct iterator_base {
  using difference_type = ll;
  using value_type = T;
  using pointer = T *;
  using reference = T &;
  using iterator_category = random_access_iterator_tag;
  ll i;
  iterator_base(ll const &i) : i(i) {}
  bool operator==(Itr const &itr) const { return i == itr.i; }
  bool operator!=(Itr const &itr) const { return i != itr.i; }
  ll operator-(Itr const &itr) const { return i - itr.i; }
  Itr operator++(int) { return with(i++); }
  Itr operator--(int) { return with(i--); }
  Itr operator+(ll const &n) const { return with(i + n); }
  Itr operator-(ll const &n) const { return with(i - n); }
  Itr &operator+=(ll const &n) {
    i += n;
    return self();
  }
  Itr &operator-=(ll const &n) { return self() += (-n); }
  Itr &operator++() { return self() += 1; }
  Itr &operator--() { return self() -= 1; }

protected:
  virtual Itr with(ll i) const = 0;
  virtual Itr &self() = 0;
};
template <class T> struct seq_base {
  ll const b;
  ll const e;
  seq_base(ll b, ll e) : b(b), e(max(b, e)) {}
  ll size() const { return e - b; }
  bool empty() const { return size() == 0; }
};
template <class T> struct seq : seq_base<T> {
  struct iterator : iterator_base<T, iterator> {
    seq<T> const &s;
    iterator(seq<T> const &s, ll const &i)
        : iterator_base<T, iterator>(i), s(s) {}
    iterator(iterator const &) = default;
    iterator with(ll i) const override { return iterator(s, i); }
    iterator &self() override { return *this; }
    T operator*() const { return s[this->i]; }
    iterator &operator=(iterator const &itr) {
      this->i = itr.i;
      return *this;
    }
  };
  fun<T(ll)> const f;
  seq(ll b, ll e, fun<T(ll)> const &f) : seq_base<T>(b, e), f(f) {}
  iterator begin() const { return iterator(*this, 0); }
  iterator end() const { return iterator(*this, this->size()); }
  T operator[](ll i) const { return f(i + this->b); }
};

struct range : seq_base<ll> {
  struct iterator : iterator_base<ll, iterator> {
    iterator(ll const &i) : iterator_base<ll, iterator>(i) {}
    iterator with(ll i) const override { return iterator(i); }
    iterator &self() override { return *this; }
    ll operator*() const { return i; }
  };
  range(ll n) : range(0, n) {}
  range(ll b, ll e) : seq_base<ll>(b, e) {}
  iterator begin() const { return iterator(b); }
  iterator end() const { return iterator(e); }
  bool has(ll x) const { return b <= x && x < e; }
  ll operator[](ll i) const { return i + b; }
};
} // namespace ranges
using range = ranges::range;
range::iterator end(ll i) { return range::iterator(i); }
range::iterator begin(ll i = 0) { return range::iterator(i); }
template <class F> auto seq(ll n, F const &f) {
  using T = decltype(f(declval<ll>()));
  return ranges::seq<T>(0LL, n, f);
}

namespace optional {
template <class T> struct opt {
  std::unique_ptr<T> ptr;
  opt(nullptr_t ptr = nullptr) : ptr(ptr){};
  opt(T const &val) : ptr(make_unique<T>(val)){};
  opt(opt<T> const &o) : opt<T>(nullptr) {
    if (o)
      ptr = make_unique<T>(*o);
  }
  opt(opt<T> &&) = default;
  opt<T> &operator=(opt<T> const &o) {
    ptr = o ? make_unique<T>(*o) : nullptr;
    return *this;
  }
  opt<T> &operator=(opt<T> &&) = default;
  operator bool() const { return has_value(); }
  T &operator*() { return value(); }
  T const &operator*() const { return value(); }
  T *operator->() { return &(*ptr); }
  T const *operator->() const { return &(*ptr); }
  T &value() { return *ptr; }
  T const &value() const { return *ptr; }
  T value_or(T const &val) const { return (ptr) ? value() : val; }
  bool has_value() const { return ptr != nullptr; }
};
template <class T> io::OS &operator<<(io::OS &o, opt<T> const &opt) {
  using io::operator<<;
  return opt.has_value() ? (o << "Some(" << opt.value() << ")") : (o << "None");
}
} // namespace optional
using optional::opt;

bool odd(ll n) { return n & 1; }
bool even(ll n) { return !odd(n); }

bool imply(bool p, bool q) { return !p || q; }
bool iff(bool p, bool q) { return p == q; }

constexpr lf PI = 3.141592653589793238462643383279502884L;
constexpr ll MOD = 1e9 + 7;

#endif /* TEMPLATE_HPP */

/**
// DP Definition
using Data = struct {};
using Key = tuple<>;
using Value = ll;
using Cache = u_map<Key, Value>;
struct DP {
  Data data;
  Cache cache;
  DP(Data const &data) : data(data), cache((ll)1e6) {}
  Value compute(Key const &key) {
    if (IS_INITIAL)
      return INITIAL_VALUE;
    auto found = cache.find(key);
    if (found != cache.end())
      return found->second;
    return cache[key] = RECURSION_CALL(this->compute(Key{}));
  }
};
// DP Call
/*
  DP dp(Data{});
  print(dp.compute(Key{}));
*/

void solve();
int main(int, char *[]) {
  init_io();
  ll t = 1;
  /** input(t); /**/
  while (t--)
    solve();
  cout.flush();
}

void solve() {
  str S;
  input(S);
  if (S == "zyxwvutsrqponmlkjihgfedcba") {
    print(-1);
    return;
  }
  std::set<char> alphabets(begin('a'), end('z' + 1));
  std::set<str> ans;
  for (ll i = 0; i <= S.size(); i++) {
    {
      str T = S.substr(0, i);
      auto A = alphabets;
      for (auto &&c : T)
        A.erase(c);
      A.erase(S[i]);
      T += *A.begin();
      dump(i, T);
      if (T > S)
        ans.insert(T);
    }
    {
      str T = S.substr(0, i);
      auto A = alphabets;
      for (auto &&c : T)
        A.erase(c);
      T += *A.begin();
      dump(i, T);
      if (T > S)
        ans.insert(T);
    }
  }
  dump(ans);
  print(*ans.begin());
}

Submission status: Runtime Error

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

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

d={}
e={}
for i in range(len(alp)):
    d[alp[i]]=i
    e[i]=alp[i]

n=len(s)
if n!=26:
    ans=[]
    for i in range(n):
        ans.append(d[s[i]])

    for j in range(26):
        if j not in ans:
            ans.append(j)
            break

    st=''
    for k in ans:
        st+=e[k]
else:
    indices=[0,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]
    dp=[]
    flgs=[0]*n

    for i in range(n):
        dp.append(min(indices))
        if dp[i]==d[s[i]]:
            flgs[i]=0
        else:
            flgs[i]=1
        indices.remove(d[s[i]])

    indices=[0,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]
    ans=[]
    for j in range(n):
        if flgs[j]==0 and flgs[j+1]==0:
            ans.append(d[s[j]])
            indices.remove(d[s[j]])
        else:
            indices.remove(d[s[j]])
            ans.append(min(indices))
            break

    st=''
    for k in ans:
        st+=e[k]


print(st)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
S = input()

# 1 <= |S| <= 26
# lowercase

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            # print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    # print("checking:", j)
                    # print("S[i], S[j]", S[i], ",", S[j])
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        # S[i+1:] = S[:i] + "".join(list(reversed(S[i+1:])))
                        # S = S[:i] + 
                        np += "".join(list(reversed(S[i+1:])))
                        break

        # print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Submission status: Runtime Error

Solution code:
S = input()

# 1 <= |S| <= 26
# lowercase

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    # print("checking:", j)
                    # print("S[i], S[j]", S[i], ",", S[j])
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        # S[i+1:] = S[:i] + "".join(list(reversed(S[i+1:])))
                        # S = S[:i] + 
                        np += "".join(list(reversed(S[i+1:])))
                        break

        # print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Submission status: Runtime Error

Solution code:
S = input()

# 1 <= |S| <= 26
# lowercase

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    # print("checking:", j)
                    # print("S[i], S[j]", S[i], ",", S[j])
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        # S[i+1:] = S[:i] + "".join(list(reversed(S[i+1:])))
                        # S = S[:i] + 
                        np += "".join(list(reversed(S[i+1:])))
                        break

        print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Submission status: Runtime Error

Solution code:
S = input()

import sys
import copy

if len(S) == 26:
    if S == "zyxwvutsrqponmlkjihgfedcba":
        ans = "-1"
        print(ans)
        sys.exit()

    else:
        original = copy.copy(S)
        np = ""
        for i in range(0, len(S)-2):
            print(i)
            if S[i] < S[i+1]:
                np += S[i]

            else:
                i = i - 1
                for j in reversed(range(i, len(S))):
                    if S[i] < S[j]:
                        np = np[:-1] + S[j]
                        tmp = S[i]
                        S = S[:i-1] + S[j] + S[i+1:]
                        S = S[:j-1] + tmp + S[j+1:]
                        np += "".join(list(reversed(S[i+1:])))
                        break

        print("np", np)
        for b in range(0, 27):
            if np[b] != original[b]:
                print(np[:b+1])
                exit()

        print(S)

            
else:
    hist = set(S)
    for c in range(0, 27):
        cand = chr(c+ord('a'))
        if cand not in hist:
            print(S+cand)
            sys.exit()

Submission status: Runtime Error

Solution code:
s = input()

abc = ['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 s == ''.join(abc)[::-1]:
    print(-1)
    exit()
if len(s) != 26:
    for a in abc:
        if a not in abc:
            print(s+a)
            exit()
i = 25
while s[i-1] > s[i]:
    i -= 1
    #print(s[i-1], s[i])
#print(s[i-1], s[i])
tt = s[i-1]
ss = list(s[i-1:])
ss.sort()
print(s[:i-1]+ss[ss.index(tt)+1])

Submission status: Runtime Error

Solution code:
s = input()

abc = ['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 s == ''.join(abc)[::-1]:
    print(-1)
    exit()
if len(s) <26:
    for a in abc:
        if a not in abc:
            print(s+a)
            exit()
i = 25
while s[i-1] > s[i]:
    i -= 1
    #print(s[i-1], s[i])
#print(s[i-1], s[i])
tt = s[i-1]
ss = list(s[i-1:])
ss.sort()
print(s[:i-1]+ss[ss.index(tt)+1])

Submission status: Runtime Error

Solution code:
import sys
s=input()
if len(s)==26:
    if s=="zyxwvutsrqponmlkjihgfedcba":
        print(-1)
        sys.exit()
    for i in range(25):
        if ord(s[i])+1==ord(s[i+1]):
            continue
        else:
            s=s[:i]
            break
    se=set(s)
    p=sorted(list(set(s)))
    moji=[]
    alphabet=[chr(i) for i in range(97, 97+26)]
    for i in range(26):
        if alphabet[25-i] not in se:
            
            moji.append(alphabet[25-i])
    print(s+moji[len(moji)-2])
else:
    se=set(s)
    p=sorted(list(set(s)))
    alphabet=[chr(i) for i in range(97, 97+26)]
    for i in range(26):
        if alphabet[25-i] not in se:
            moji=alphabet[25-i]
    print(s+moji)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
S = list(input())
c = set(S)
all_ar = list("abcdefghijklmnopqrstuvwxyz")
if len(c) == 26:
    print(-1)
    exit()
for i in range(26):
    if not all_ar[i] in c:
        print(''join(S)+all_ar[i])
        exit()

Submission status: Runtime Error

Solution code:
s = input()
wl = [chr(i) for i in range(97,123)]
hoge = 'zyxwvutsrqponmlkjihgfedcba'
if s == hoge:
  print(-1)
  exit()
if len(s) < 26:
  for w in wl:
    if not w in s:
      s += w
      print(s)
      exit()
else:
  for i in range(26):
    for j in range(ord(s[-i-1])+1, 97+26):
      if chr(j) not in s[:-i-1]:
        print(S[:-i-1] + chr(j))

Submission status: Runtime Error

Solution code:
s = input()
wl = [chr(i) for i in range(97,123)]
if len(s) < 26:
  for w in wl:
    if not w in s:
      s += w
      print(s)
      exit()
else:
  for i in range(26):
    if s[i] != wl[26-i]:
      s = s[:i] + chr(ord(s[i])+1)
      print(s)
      exit()
print(-1)

Submission status: Runtime Error

Solution code:
s = input()
if s == "zyxwvutsrqponmlkjihgfedcba":
    print(-1)
elif len(s) != 26:
    used = [False] * 26
    for i in range(len(s)):
        used[ord(s[i]) - ord("a")] = True
    for i in range(26):
        if used[i] is False:
            s += chr(ord("a") + i)
            print(s)
            break
else:
    ss = []
    s = list(s)
    done = False
    used = [False] * 26
    for i in range(25):
        #print("load", s[i])
        if s[i] > s[i+1]:
            used[ord(s[i]) - ord("a")] = True
            #print(s[i])
            for i in range(26):
                if used[i] is False:
                    ss[-1] = chr(ord("a") + i)
                    print("".join(ss))
                    done = True
                    break
                if done:
                    break
        else:
            ss += s[i]
            used[ord(s[i]) - ord("a")] = True

Submission status: Runtime Error

Solution code:
s = str(input())
alpha = 'abcdefghijklmnopqrstuvwxyz'
if len(s) == 26:
    if s == 'zyxwvutsrqponmlkjihgfedcba':
        print(-1)
    else:
        for i in range(26):
            if ord(s[i+1]) - ord(s[i]) != 1:
                S = sorted(set(s) - set(s[:i+1]))
                print(s[:i] + S[0])
                break
else:
    S = sorted(set(alpha) - set(s))
    print(s + S[0])

Submission status: Runtime Error

Solution code:
import sys
s = deque(map(str, input().rstrip()))

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

s = [ord(x) for x in s]

if len(s) < 26:
    can_use = set(range(97, 123)) - set(s)
    s = s + [min(can_use)]

else:
    add = []
    cnt = 0
    while s:
        cnt += 1
        q = s.pop()
        add.append(q)
        if q == 122:
            break
    add = [x for x in add if x > s[-1]]
    
    if cnt != 1:
        s[-1] = min(add)

s = [chr(x) for x in s]

print("".join(s))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
from collections import Counter
s = deque(map(str, input().rstrip()))

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

s = [ord(x) for x in s]

if len(s) < 26:
    can_use = set(range(97, 123)) - set(s)
    s = s + [min(can_use)]

else:
    add = []
    while s:
        q = s.pop()
        if q == 122:
            break
        else:
            add.append(q)

    s[-1] = min(add)

s = [chr(x) for x in s]

print("".join(s))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
from collections import Counter
s = deque(map(str, input().rstrip()))

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

s = [ord(x) for x in s]

if len(s) == 26:
    maxlen = True
    s = s[::-1]
    x = s[0]
    for i, w in enumerate(s):
        if x > w:
            break

    s = s[::-1]
    s = s[:-i - 1]

can_use = set(range(97, 123)) - set(s)
if maxlen:
    can_use = can_use - {min(can_use)}
s = s + [min(can_use)]

s = [chr(x) for x in s]

print("".join(s))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
from collections import Counter
s = deque(map(str, input().rstrip()))

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

s = [ord(x) for x in s]

if len(s) == 26:
    maxlen = True
    s = s[::-1]
    x = s[0]
    for i, w in enumerate(s):
        if x > w:
            break

    s = s[::-1]
    s = s[:-i - 1]

can_use = set(range(97, 123)) - set(s)
if maxlen:
    s = s + [min(can_use) + 1]
else:
    s = s + [min(can_use)]

s = [chr(x) for x in s]

print("".join(s))

Submission status: Runtime Error

Solution code:
from copy import copy, deepcopy # pythonのみ．1次元はcopy．多次元はdeepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():

        def next_permutation(a):
            """Generate the lexicographically next permutation inplace.

            https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
            Return false if there is no next permutation.
            """
            # Find the largest index i such that a[i] < a[i + 1]. If no such
            # index exists, the permutation is the last permutation
            for i in reversed(range(len(a) - 1)):
                if a[i] < a[i + 1]:
                    break  # found
            else:  # no break: not found
                return False  # no next permutation

            # Find the largest index j greater than i such that a[i] < a[j]
            j = next(j for j in reversed(range(i + 1, len(a))) if a[i] < a[j])

            # Swap the value of a[i] with that of a[j]
            a[i], a[j] = a[j], a[i]

            # Reverse sequence from a[i + 1] up to and including the final element a[n]
            a[i + 1:] = reversed(a[i + 1:])
            return True

        alpha = list(chr(ord('a')+i) for i in range(26))

        S = input()
        if len(S) < 26:
            for i in range(len(S)):
                alpha.remove(S[i])
            return S+alpha[0]
        else:
            S = list(S)
            P = S.deepcopy()
            next_permutation(P)
            for i in range(26):
                if P[i] != S[i]:
                    return ''.join(P[:i+1])
            return -1
    print(main())

resolve()