Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 296 ms):
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        sc.nextLine(); 
        while (t-- > 0) {
            String s = sc.nextLine();
            char[] arr = s.toCharArray();
            int swapcount = 1;
            int ans = 0;
            while (swapcount != 0) {
                int sw = 0;
                for (int i = 1; i < arr.length; i++) {
                    char a = (char) (arr[i] - 1);
                    char b = arr[i - 1];
                    if ((arr[i] != '1' || arr[i] != '0') && a - b > 0) {
                        if (arr[i] > arr[i - 1]) {
                            swap(i - 1, i, arr);
                            sw++;
                        }
                    }
                }

                if (sw == 0) {
                    break;
                }
            }
            System.out.println(new String(arr));
        }
    }

    public static void swap(int i, int j, char[] arr) {
        arr[j] = (char) (arr[j] - 1);
        char t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
}


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;

    }

}