Problem: 2050D - Digital string maximization
Language: java

Solution 1 (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);

        }

    }

}

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();

}

}