C# Solution for Remove K Digits Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this approach is to simulate the process of removing k digits whil
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this approach is to simulate the process of removing k digits while maintaining the smallest possible number.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tIterate through each digit in the input string.\n2.\tUse a StringBuilder to build the result incrementally.\n3.\tCompare each digit with the top element of the result.\n\t\u2022\tIf the current digit is smaller than the top element of the result, remove the top element and continue this process until either the StringBuilder is empty or we\u2019ve removed enough digits (k) or the top element is smaller or equal to the current digit.\n\t\u2022\tAppend the current digit to the StringBuilder.\n4.\tAfter iterating through all digits, if there are still digits to be removed, remove them from the end of the StringBuilder.\n5.\tFinally, remove any leading zeros from the StringBuilder.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe algorithm iterates through each digit in the input string once, resulting in O(n) time complexity, where n is the length of the input string.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nWe use a StringBuilder to store the result, which can grow up to the length of the input string. Hence, the space complexity is O(n), where n is the length of the input string.\n\n# Code\n```\npublic class Solution {\n    public string RemoveKdigits(string num, int k) {\n        if (num.Length == k) return "0"; // Remove all digits\n        \n        StringBuilder result = new StringBuilder();\n        int digitsToRemove = k;\n        \n        foreach (char digit in num) {\n            while (digitsToRemove > 0 && result.Length > 0 && result[result.Length - 1] > digit) {\n                result.Remove(result.Length - 1, 1); // Remove larger digits to minimize the number\n                digitsToRemove--;\n            }\n            result.Append(digit);\n        }\n        \n        // Remove remaining digits if k > 0\n        while (digitsToRemove > 0) {\n            result.Remove(result.Length - 1, 1);\n            digitsToRemove--;\n        }\n        \n        // Remove leading zeros\n        while (result.Length > 1 && result[0] == \'0\') {\n            result.Remove(0, 1);\n        }\n        \n        return result.ToString();\n    }\n}\n```
['C#']