Problem: 2050D - Digital string maximization
Language: java

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

public class D_Digital_string_maximization {
    public static void main(String[] args) throws IOException {
        FastIO io = new FastIO();
        int t = io.nextInt();
        while(t-- > 0) {
            String s = io.next();
            int n = s.length();
            int[] a = new int[n];
            for(int i = 0; i < n; i++) a[i] = (int)(s.charAt(i) - '0');
            for (int i = 0; i < n - 1; i++) {
                boolean curr = true;
                for (int j = 0; j < n - i - 1; j++) {
                    if (a[j]+1 < a[j + 1]) {
                        int temp = a[j];
                        a[j] = a[j + 1]-1;
                        a[j + 1] = temp;
                        curr = false;
                    }
                }
                if(curr) break;
            }
            StringBuilder res = new StringBuilder();
            for(int i: a) res.append(i);
            io.println(res);
        }
        io.close();
    }
    
    static class FastIO {
        BufferedReader br;
        PrintWriter pw;
        StringTokenizer st;

        public FastIO() {
            br = new BufferedReader(new InputStreamReader(System.in));
            pw = new PrintWriter(new BufferedOutputStream(System.out));
        }

        public FastIO(String inputFile, String outputFile) throws IOException {
            br = new BufferedReader(new FileReader(inputFile));
            pw = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
        }

        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;
        }

        void print(Object obj) {
            pw.print(obj);
        }

        void println(Object obj) {
            pw.println(obj);
        }

        void close() {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            pw.close();
        }
    }
}


Solution 2 (runtime: 858 ms):
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class Solution {

    static void tc(Scanner sc){
        String str = sc.next();
        Stack<Integer> ans = new Stack<>();
        for(int i=0; i<str.length(); i++){
            int x = str.charAt(i)-'0';
            
            Stack<Integer> temp = new Stack<>();
            while (ans.isEmpty()==false && ans.peek() < x-1) {
                x--;
                temp.add(ans.pop());
            }
            ans.add(x);
            while (temp.isEmpty()==false) {
                ans.add(temp.pop());
            }
        }
        for(int x:ans){
            System.out.print(x);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            // System.out.println(t);
            tc(sc);
        }
        sc.close();
    }
}