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: 609 ms):
import java.math.*;

import java.util.*;



public class Main {

static int N = (int)2e5 + 5;

static int[] a = new int[N];

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int T = sc.nextInt();

while(T-- > 0){

String s = sc.next();

int len = s.length();

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

a[i] = (int)s.charAt(i - 1) - (int)'0';

}

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

int x = a[i], idx = i;

int mx = x, id = -1;

for(int j = i + 1;j <= len && j <= i + 8;j++){

if(a[j] - (j - i) > mx){

mx = a[j] - (j - i);

idx = j;

}

}

for(int j = idx - 1;j >= i;j--){

int t = a[j];

a[j] = a[j + 1] - 1;

a[j + 1] = t;

}

}

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

System.out.print(a[i]);

}

System.out.println();

}

sc.close();

}

}