Problem: 2050D - Digital string maximization
Language: java

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

public class DigitalStringMaximization {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tokenizer = new StringTokenizer(in.readLine());

        int t = Integer.parseInt(tokenizer.nextToken());

        while (t-- > 0) {
            tokenizer = new StringTokenizer(in.readLine());
            char[] c = tokenizer.nextToken().toCharArray();

            for (int i = c.length - 1; i > 0; i--) {
                if (c[i] > c[i - 1] + 1) {
                    operation(c, i);
                    i += 9;
                    if (i > c.length) {
                        i = c.length;
                    }
                }
            }

            System.out.println(new String(c));
        }
    }

//    public static int findLast(char[] c) {
//        for (int i = c.length - 1; i > 0; i--) {
//            if (c[i] > c[i - 1] + 1) {
//                return i + 1;
//            }
//        }
//        return c.length;
//    }

    public static void operation(char[] c, int i) {
        int cur = c[i];
        int left = c[i - 1];
        c[i - 1] = (char) (cur - 1);
        c[i] = (char) left;
    }
}

Solution 2 (runtime: 234 ms):
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.StringTokenizer;



public class round991 {

static class Pair implements Comparable<Pair>{

int a;

long b;

String str;



Pair(int a,long b,String str){

this.a=a;

this.b=b;

this.str=str;

}



public int compareTo(Pair o) {

return Integer.compare(this.a, o.a); // Ascending order by a

}

//

//            public int compareTo(Pair o){

//                return Long.compare(this.b,o.b);

//            }

//

//            public int compareTo(Pair other) {

//                return this.str.compareTo(other.str); // Ascending order by str

//            }

//

}

static class FastReader {

BufferedReader br;

StringTokenizer st;



public FastReader() {

br = new BufferedReader(

new InputStreamReader(System.in));

}



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 {

if (st.hasMoreTokens()) {

str = st.nextToken("\n");

} else {

str = br.readLine();

}

} catch (IOException e) {

e.printStackTrace();

}

return str;

}

}

static ArrayList<Integer>[] gr;

public static void main(String[] args) {

FastReader sc = new FastReader();

PrintWriter out=new PrintWriter(System.out);

int t = sc.nextInt();

while (t != 0) {

t--;

String str=sc.next();

int[] arr=new int[str.length()];

for (int i = 0; i < arr.length; i++) {

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

}

StringBuilder ans=new StringBuilder("");

for (int i = 0; i < arr.length; i++) {

int dig=arr[i];

int j=i-1;

while (j>=0 && arr[j]<dig-1){

arr[j+1]=arr[j];

--dig;

arr[j]=dig;

--j;

}

//                out.println(st);

}

//            out.println(Arrays.toString(arr));

for (int num:arr) {

ans.append(""+num);

}

out.println(ans);

}

out.flush();

}

}