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: 296 ms):


import java.util.*;

import java.io.*;



public class DigitalStringMax {



public static void main(String args[]) throws Exception {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

PrintWriter pw = new PrintWriter(System.out);



int tt = Integer.parseInt(br.readLine());

while(tt-- > 0) {



char ch[] = br.readLine().toCharArray();



int n = ch.length;

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

int max = -1;

int ind = -1;

for(int j = i;j < Math.min(n,i+9); j++){



if(max < (ch[j] - (j - i))){

max = (ch[j] - (j - i));

ind = j;

}



}



ch[ind] = (char)(ch[ind]  - (ind - i ));

for(int j = ind; j > i; j--){

char temp = ch[j];



ch[j] = ch[j-1];

ch[j-1] = temp;



}

}



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

pw.print(ch[i]);

}

pw.println();



}

pw.flush();

}

}