Java solution based on array of numbers
\n# Code\n\nclass Solution {\n    public int minMaxDifference(int num) {\n        final String strNum = String.valueOf(num);\n        final int length = strNum.
\n# Code\n```\nclass Solution {\n    public int minMaxDifference(int num) {\n        final String strNum = String.valueOf(num);\n        final int length = strNum.length();\n        final char[] chars = strNum.toCharArray();\n        final int[] nums = new int[length];\n        for (int i = 0; i < length; i++) {\n            nums[i] = Character.getNumericValue(chars[i]);\n        }\n\n        // find first non-9 digit\n        int first = 0;\n        for (int i = 0; i < length; i++) {\n            if (nums[i] != 9) {\n                first = nums[i];\n                break;\n            }\n        }\n\n        // prepare power cache\n        final int[] powers = new int[length];\n        for (int i = 0; i < length; i++) {\n            powers[i] = (int) Math.pow(10, length - i - 1);\n        }\n\n        int max = 0;\n        for (int k = 0; k < length; k++) {\n            max += (nums[k] == first ? 9 : nums[k]) * powers[k];\n        }\n        \n        // replace first with 0\n        final int second = nums[0];\n        int min = 0;\n        \n        // don\'t need to count first and all replaced\n        for (int k = 1; k < length; k++) {\n            min += (nums[k] == second ? 0 : nums[k] * powers[k]);\n        }\n\n        return max - min;\n    }\n}\n```
['Java']