Java 0 ms beats 100%
Complexity

Time complexity:
O(n)


Space complexity:
O(1)

Code
# Complexity
- Time complexity:
$$O(n)$$

- Space complexity:
$$O(1)$$

# Code
```java []
class Solution {
    public int minMaxDifference(int num) {
        var strNum = String.valueOf(num);
        var firstDigit = strNum.charAt(0);
        var firstLessNineDigit = '9';

        for (var ch : strNum.toCharArray()) {
            if (ch != '9') {
                firstLessNineDigit = ch;
                break;
            }
        }
        int max = 0, min = 0;
        for (var ch : strNum.toCharArray()) {
            if (ch == firstLessNineDigit) {
                max = 10 * max + 9;
            } else {
                max = 10 * max + (ch - '0');
            }
        }

        for (var ch : strNum.toCharArray()) {
            if (ch == firstDigit) {
                min = 10 * min;
            } else {
                min = 10 * min + (ch - '0');
            }
        }

        return max - min;
        
    }
}
```
['Java']