Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 234 ms):
import java.io.*;
import java.util.*;

public class D_Digital_string_maximization {
    public static void main(String[] args) throws IOException {
        FastIO io = new FastIO();
        int t = io.nextInt();
        while(t-- > 0) {
            String s = io.next();
            int n = s.length();
            int[] a = new int[n];
            for(int i = 0; i < n; i++) a[i] = (int)(s.charAt(i) - '0');
            for (int i = 0; i < n - 1; i++) {
                boolean curr = true;
                for (int j = 0; j < n - i - 1; j++) {
                    if (a[j]+1 < a[j + 1]) {
                        int temp = a[j];
                        a[j] = a[j + 1]-1;
                        a[j + 1] = temp;
                        curr = false;
                    }
                }
                if(curr) break;
            }
            StringBuilder res = new StringBuilder();
            for(int i: a) res.append(i);
            io.println(res);
        }
        io.close();
    }
    
    static class FastIO {
        BufferedReader br;
        PrintWriter pw;
        StringTokenizer st;

        public FastIO() {
            br = new BufferedReader(new InputStreamReader(System.in));
            pw = new PrintWriter(new BufferedOutputStream(System.out));
        }

        public FastIO(String inputFile, String outputFile) throws IOException {
            br = new BufferedReader(new FileReader(inputFile));
            pw = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        void print(Object obj) {
            pw.print(obj);
        }

        void println(Object obj) {
            pw.println(obj);
        }

        void close() {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            pw.close();
        }
    }
}


Solution 2 (runtime: 327 ms):
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        while (t-- > 0) {

            String str = sc.next();

            int n = str.length();

            int arr[] = new int[n];



            for (int i = 0; i < n; i++) {

                arr[i] = str.charAt(i) - '0';

            }

            for (int i = 1; i < n; i++) {

                int idx = i;

                while (idx >= 1 && arr[idx] > 0 && arr[idx] > arr[idx - 1] + 1) {

                    int temp = arr[idx];

                    arr[idx] = arr[idx - 1];

                    arr[idx - 1] = temp - 1;

                    idx--; 

                }

            }

            StringBuilder sb = new StringBuilder(n);

            for (int a : arr) {

                sb.append(a);

            }

            System.out.println(sb);

        }

    }

}