Problem: 2050D - Digital string maximization
Language: java

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

public class DigitalStringMaximization{

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] result = new String[n];
		for(int i = 0; i < n; i++){
			result[i]= maximize(sc.next());
		}
		Arrays.stream(result).forEach(System.out::println);
	}
	
	private static String maximize(String str){
		char[] chars = str.toCharArray();
		int num;
		for(int i = 1; i < chars.length; i++){
			num = chars[i] - '0';
			for(int j = num; j >= 0; j--){
				if( i - j < 0) continue;
				if(chars[ i - j ] < (char)(chars[i] - j)){
					char temp = (char)(chars[i] - j);
					for(int k = i; k > i - j; k--){
						chars[k] = chars[k - 1];
					}
					chars[i - j] = temp;
					break;
				}
			}
		} 
		return new String(chars);
	}
	
	
}

/*
i = 1 51476
i = 2 53176
i = 3 53616 55316
I = 4 55316
i = 5 55351 55431
19


*/


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;

    }

}