C# solution with stringbuilder
Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\ncsharp []\npublic class Solution \n{\n    public int MinMaxDifference(int num) \n  
# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```csharp []\npublic class Solution \n{\n    public int MinMaxDifference(int num) \n    {\n        StringBuilder sb = new StringBuilder(num.ToString());\n        char prev = \'9\';\n        for (int i = 0; i < sb.Length; i++)\n        {\n            char c = sb[i];\n            if (c != \'9\' && prev == \'9\')\n            {\n                prev = c;\n                sb[i] = \'9\';\n            }\n            else\n            {\n                if (sb[i] == prev) { sb[i] = \'9\'; }\n            }\n        }\n\n        int max = Int32.Parse(sb.ToString());\n\n        sb = new StringBuilder(num.ToString());\n        prev = \'0\';\n        for (int i = 0; i < sb.Length; i++)\n        {\n            char c = sb[i];\n            if (c != \'0\' && prev == \'0\')\n            {\n                prev = c;\n                sb[i] = \'0\';\n            }\n            else\n            {\n                if (sb[i] == prev) { sb[i] = \'0\'; }\n            }\n        }\n        int min = Int32.Parse(sb.ToString());\n        return max - min;\n    }\n}\n```
['C#']