Language: Java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static int modP = 1000000007;

	static class TaskX {
		public void solve(int testNumber, InputReader in, PrintWriter out) {

			char[] s = in.nextString().toCharArray();
			int n = s.length;
			Set<Character> set = new HashSet<>();
			for (int i = 0; i < n; i++) {
				set.add(s[i]);
			}

			if (n < 26) {
				for (char c = 'a'; c <= 'z'; c++) {
					if (!set.contains(c)) {
						out.println(new String(s) + c);
						return;
					}
				}
			} else {
				int pnt = n - 1;
				set.remove(s[pnt]);
				pnt--;

				while (pnt >= 0) {
					if (s[pnt] > s[pnt + 1]) {
						set.remove(s[pnt]);
						pnt--;
						continue;
					}
					for (char c = (char)(s[pnt] + 1); c <= 'z'; c++) {
						if (!set.contains(c)) {
							out.println(new String(Arrays.copyOf(s, pnt - 1)) + c);
							return;
						}
					}
				}
			}
			out.println(-1);
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}

	}

}
package agc022;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Set;
import java.util.StringTokenizer;

public class A_2 {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static int modP = 1000000007;

	static class TaskX {
		public void solve(int testNumber, InputReader in, PrintWriter out) {

			char[] s = in.nextString().toCharArray();
			int n = s.length;
			Set<Character> set = new HashSet<>();
			for (int i = 0; i < n; i++) {
				set.add(s[i]);
			}

			if (n < 26) {
				for (char c = 'a'; c <= 'z'; c++) {
					if (!set.contains(c)) {
						out.println(new String(s) + c);
						return;
					}
				}
			} else {
				int pnt = n - 1;
				set.remove(s[pnt]);
				pnt--;

				while (pnt >= 0) {
					if (s[pnt] > s[pnt + 1]) {
						set.remove(s[pnt]);
						pnt--;
						continue;
					}
					for (char c = (char)(s[pnt] + 1); c <= 'z'; c++) {
						if (!set.contains(c)) {
							out.println(new String(Arrays.copyOf(s, pnt - 1)) + c);
							return;
						}
					}
				}
			}
			out.println(-1);
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}

	}

}


Compiler output:
Main.java:120: error: class, interface, enum, or record expected
package agc022;
^
Main.java:122: error: class, interface, enum, or record expected
import java.io.BufferedReader;
^
Main.java:123: error: class, interface, enum, or record expected
import java.io.IOException;
^
Main.java:124: error: class, interface, enum, or record expected
import java.io.InputStream;
^
Main.java:125: error: class, interface, enum, or record expected
import java.io.InputStreamReader;
^
Main.java:126: error: class, interface, enum, or record expected
import java.io.OutputStream;
^
Main.java:127: error: class, interface, enum, or record expected
import java.io.PrintWriter;
^
Main.java:128: error: class, interface, enum, or record expected
import java.util.Arrays;
^
Main.java:129: error: class, interface, enum, or record expected
import java.util.HashSet;
^
Main.java:130: error: class, interface, enum, or record expected
import java.util.InputMismatchException;
^
Main.java:131: error: class, interface, enum, or record expected
import java.util.Set;
^
Main.java:132: error: class, interface, enum, or record expected
import java.util.StringTokenizer;
^
12 errors