Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 827 ms):
import java.io.*;
import java.util.*;
public class Main {
    public static final int MOD = 998244353;
    static PrintWriter out;
    static FastReader in;
    static long[] fact = new long[200005];
    static long[] inv = new long[200005];

    //    static{
//        fact[0]=1;inv[0]=1;
//        for(int i=1;i<=2e5;i++){
//            fact[i]=fact[i-1]*i%MOD;
//            inv[i]=power(fact[i],MOD-2,MOD);
//        }
//    }
    public static void main(String[] args) {
        in = new FastReader();
        out = new PrintWriter(System.out);
        int t = in.nextInt();
        while(t-->0){
            String s = in.next();
            int n = s.length();
            int[] a = new int[n];
            for(int i=0;i<n;i++){
                a[i] = Character.getNumericValue(s.charAt(i));
            }
            int idx=-1;
            int max=0;
            for(int i=0;i<n;i++){
                for(int j=i;j<Math.min(n,i+10);j++){
                    if(a[j]-(j-i)>max){
                        idx=j;
                        max = a[j]-(j-i);
                    }
                }
                if(idx!=-1){
                    for(int k=idx;k>i;k--){
                        a[k]=a[k-1];
                    }
                }
                a[i]=max;
                max=0;
                idx=-1;
            }
            for(int i=0;i<n;i++){
                out.print(a[i]);
            }
            out.println();
        }
        out.flush();
        out.close();
    }




    public static int log2(long N) {
        return 63 - Long.numberOfLeadingZeros(N);
    }
    public static long nCk(int n, int k) {
        if (k > n) return 0;
        return (fact[n] * inv[k] % MOD * inv[n-k] % MOD);
    }

    public static long power(long base, long exp, int mod) {
        long result = 1;
        while (exp > 0) {
            if ((exp & 1) == 1) {
                result = (result * base) % mod;
            }
            base = (base * base) % mod;
            exp >>= 1;
        }
        return result;
    }
    public static int binSearch(int[] arr, int key){
        int l = 0;
        int r = arr.length-1;
        while (l<=r){
            int mid = (l+r)/2;
            if (arr[mid]==key){
                return mid;
            }
            if(arr[l]<arr[mid]){
                if (key>=arr[l]&&key<arr[mid]){
                    r=mid-1;
                }
                else {
                    l=mid+1;
                }
            }
            else {
                if (key>arr[mid]&&key<=arr[r]){
                    l=mid+1;
                }
                else {
                    r=mid-1;
                }
            }
        }
        return -1;
    }

    public static boolean binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] == target) {
                return true;
            }
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return false;
    }

    public static int lower_bound(Integer[] arr,int key){
        int l = 0;
        int r = arr.length-1;
        int mid;
        while(r-l>1){
            mid = (r+l)/2;
            if(key>arr[mid]){
                l=mid+1;
            }
            else{
                r=mid;
            }
        }
        if(arr[l]>=key){
            return l;
        }
        else if(arr[r]>=key){
            return r;
        }
        return -1;
    }

    public static int upper_bound(int[] arr,int key){
        int l = 0;
        int r = arr.length-1;
        int mid;
        while(r-l>1){
            mid = (r+l)/2;
            if(key>=arr[mid]){
                l=mid+1;
            }
            else{
                r=mid;
            }
        }
        if(arr[l]>key){
            return l;
        }
        else if(arr[r]>key){
            return r;
        }
        return -1;
    }

    public static boolean inRange(int[] arr, int L, int R) {
        Arrays.sort(arr);
        long product = 1;

        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int xor = arr[i] ^ arr[j];
                product *= xor;

                if (product >= R || product==0) {
                    return false;
                }
            }
        }
        return product>L&&product<R;
    }
    public static int findIndex(long[] arr, int target) {
        int low = 0, high = arr.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;

            if (arr[mid] == target) {
                return mid; // Target found
            } else if (arr[mid] < target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }

        return low;
    }
    public static Pair<Integer,Integer> maxOcurrences(int[] arr) {
        HashMap<Integer, Integer> frequencyMap = new HashMap<>();

        // Count the frequency of each number
        for (int num : arr) {
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
        }

        // Find the number with the highest frequency
        int maxCount = 0;
        int maxNumber = -1; // Default value (if the array is empty)
        for (HashMap.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            if (entry.getValue() > maxCount) {
                maxCount = entry.getValue();
                maxNumber = entry.getKey();
            }
        }

        return new Pair<>(maxNumber,maxCount);
    }
    public static String changeCharAt(String str, int index, char newChar) {
        StringBuilder sb = new StringBuilder(str);
        sb.setCharAt(index, newChar);
        return sb.toString();
    }
    static int gcd(int a, int b){
        if(b==0){
            return a;
        }
        return gcd(b,a%b);
    }

    static Boolean[] isPrime;
    static{
        int k = 10_000_010;
        isPrime = new Boolean[k];
        Arrays.fill(isPrime, true);
        isPrime[0]=isPrime[1]=false;
        for(int i=2;i<k;i++){
            if(isPrime[i]){
                for(int j=2*i;j<k;j+=i){
                    isPrime[j]=false;
                }
            }
        }
    }
    static long exp(int a, int b){
        long ans = 1;
        long base = a;
        while(b>0){
            if((b&1)!=0){
                ans*=base;
            }
            base*=base;
            b>>=1;
        }
        return ans;
    }
    static long[] L_arr(int n) {
        long[] array = new long[n];
        for (int i = 0; i < n; i++) {
            array[i] = in.nextLong();
        }
        return array;
    }
    static <T> ArrayList<T> Al(int n) {
        ArrayList<T> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            T element = (T) in.next();
            list.add(element);
        }
        return list;
    }

    static <K, V> HashMap<K, V> map() {
        return new HashMap<>();
    }
    static <T> ArrayList<T> list() {
        return new ArrayList<>();
    }
    static <K, V> Pair<K, V> pair(K key, V value) {
        return new Pair<>(key, value);
    }

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

    static class Pair<K, V> extends AbstractMap.SimpleEntry<K, V> {
        Pair(K key, V value) {
            super(key, value);
        }
    }
}







Solution 2 (runtime: 281 ms):
import java.io.*;
import java.util.*;

public class DigitalStringMaximization {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tokenizer = new StringTokenizer(in.readLine());

        int t = Integer.parseInt(tokenizer.nextToken());

        while (t-- > 0) {
            tokenizer = new StringTokenizer(in.readLine());
            char[] c = tokenizer.nextToken().toCharArray();

            for (int i = c.length - 1; i > 0; i--) {
                if (c[i] > c[i - 1] + 1) {
                    operation(c, i);
                    i += 9;
                    if (i > c.length) {
                        i = c.length;
                    }
                }
            }

            System.out.println(new String(c));
        }
    }

//    public static int findLast(char[] c) {
//        for (int i = c.length - 1; i > 0; i--) {
//            if (c[i] > c[i - 1] + 1) {
//                return i + 1;
//            }
//        }
//        return c.length;
//    }

    public static void operation(char[] c, int i) {
        int cur = c[i];
        int left = c[i - 1];
        c[i - 1] = (char) (cur - 1);
        c[i] = (char) left;
    }
}