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

import java.util.*;



public class Main{

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

        if (System.getProperty("ONLINE_JUDGE") == null) {

            System.setIn(new FileInputStream("input.txt"));

            System.setOut(new PrintStream(new FileOutputStream("output.txt")));

        }



        FastReader sc = new FastReader();

        PrintWriter out = new PrintWriter(System.out);



        int t = sc.nextInt(); 

        //template

        while (t-- > 0) {

            String num = sc.next();

            int[] nums = new int[num.length()];

            int idx=0;

            while(idx<num.length()){

                nums[idx] = num.charAt(idx)-'0';

                idx++;

            }

            for(int i=1;i<num.length();i++){

                idx=i;

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

                    int val = nums[idx];

                    nums[idx]=nums[idx-1];

                    nums[idx-1]=val-1;

                    idx--;

                }

                //System.out.println(Arrays.toString(nums));

            }

            StringBuilder sb = new StringBuilder();

            for(int i:nums)sb.append(i);

            System.out.println(sb);

        }



        out.flush(); 

    }



    // Optimized Fast Input Reader

    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 {

                str = br.readLine();

            } catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }

}

class Pair<K,V>{

    K key;

    V value;

    public Pair(K key,V value){

        this.key=key;

        this.value=value;

    }

    public K getKey(){

        return this.key;

    }

    public V getValue(){

        return this.value;

    }

}