Beats Almost 100% Users! Multiple Approaches with Simple Explanation in Python, JAVA and C++!
Intuition\n Describe your first thoughts on how to solve this problem. \nYou\'re given a number as a string and a count of digits to remove (k). The goal is to 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nYou\'re given a number as a string and a count of digits to remove (k). The goal is to remove exactly k digits from the number to create the smallest possible new number.\n\n# Approach 1 (Beats 99.8% Users in Python, and C++)\n<!-- Describe your approach to solving the problem. -->\n1. If you need to remove all digits (k is as big as the number), just return "0" because you won\'t have anything left.\n2. Go through each digit of the number and if the current digit is smaller than the last digit in your "new number," throw away the last digit until it\'s not bigger anymore.\n4. Keep doing this until you\'ve thrown away k digits or you\'ve gone through all the digits.\n5. If you still have to throw away more digits (k > 0) after going through all the digits, just remove them from the end.\n6. The result is the smallest number you can make after all these operations. Handle any leading zeros.\n\n```Python3 []\nclass Solution {\n    public String removeKdigits(String num, int k) {\n\n        if (k >= num.length()) {\n            return "0";\n        }\n\n        StringBuilder result = new StringBuilder();\n\n        for (int i = 0; i < num.length(); i++) {\n            while (k > 0 && result.length() > 0 && result.charAt(result.length() - 1) > num.charAt(i)) {\n                result.deleteCharAt(result.length() - 1);\n                k--;\n            }\n\n            if (k == 0) {\n                result.append(num.substring(i));\n                break;\n            } else {\n                result.append(num.charAt(i));\n            }\n        }\n\n        while (k > 0) {\n            result.deleteCharAt(result.length() - 1);\n            k--;\n        }\n\n        String resultStr = result.toString().replaceFirst("^0+", "");\n\n        return resultStr.isEmpty() ? "0" : resultStr;\n    }\n}\n```\n```JAVA []\nclass Solution {\n    public String removeKdigits(String num, int k) {\n\n        if (k >= num.length()) {\n            return "0";\n        }\n\n        StringBuilder result = new StringBuilder();\n\n        for (int i = 0; i < num.length(); i++) {\n            while (k > 0 && result.length() > 0 && result.charAt(result.length() - 1) > num.charAt(i)) {\n                result.deleteCharAt(result.length() - 1);\n                k--;\n            }\n\n            if (k == 0) {\n                result.append(num.substring(i));\n                break;\n            } else {\n                result.append(num.charAt(i));\n            }\n        }\n\n        while (k > 0) {\n            result.deleteCharAt(result.length() - 1);\n            k--;\n        }\n\n        String resultStr = result.toString().replaceFirst("^0+", "");\n\n        return resultStr.isEmpty() ? "0" : resultStr;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    std::string removeKdigits(std::string num, int k) {\n        if (k >= num.length()) {\n            return "0";\n        }\n\n        std::vector<char> result;\n\n        for (char digit : num) {\n            while (k > 0 && !result.empty() && result.back() > digit) {\n                result.pop_back();\n                k--;\n            }\n\n            result.push_back(digit);\n        }\n\n        // Remove remaining k digits from the end\n        result.resize(result.size() - k);\n\n        // Convert vector to string and handle leading zeros\n        std::string result_str(result.begin(), result.end());\n        size_t nonZeroIndex = result_str.find_first_not_of(\'0\');\n        return (nonZeroIndex == std::string::npos) ? "0" : result_str.substr(nonZeroIndex);\n    }\n};\n```\n\n\n# Approach 2 (Beats 99.91 Users in Python3, 97.21% in JAVA)\n<!-- Describe your approach to solving the problem. -->\n1. If k is greater than or equal to the length of the number (num), it means we need to remove all digits. In this case, return "0" since there will be nothing left.\n2. Initialize an empty list (result) to store the digits of the new number.\n3. Iterate through each digit of the input number (num).\n4. While k is greater than 0 and there are digits in the result list, and the last digit in the result list is greater than the current digit from num, pop the last digit from the result list. This step is crucial for creating the smallest number.\n5. After the iteration, if there are still remaining k digits to be removed, remove them from the end of the result list.\n6. Convert the result list to a string and remove any leading zeros.\n7. Return the resulting string, or "0" if the string is empty.\n\n\n```python []\nclass Solution:\n    def removeKdigits(self, num: str, k: int) -> str:\n        n = len(num)\n\n        if k >= n:\n            return "0"\n\n        result = []\n\n        for i in range(n):\n            digit = num[i]\n\n            while k > 0 and result and result[-1] > digit:\n                result.pop()\n                k -= 1\n\n            result.append(digit)\n\n        result = result[:-k] if k > 0 else result\n\n        result_str = \'\'.join(result).lstrip(\'0\')\n\n        return result_str if result_str else \'0\'\n\n\n```\n\n```JAVA []\nclass Solution {\n    public String removeKdigits(String num, int k) {\n\n                int n = num.length();\n        \n        if (k >= n) {\n            return "0";\n        }\n\n        StringBuilder result = new StringBuilder();\n\n        for (int i = 0; i < n; i++) {\n            char digit = num.charAt(i);\n\n            while (k > 0 && result.length() > 0 && result.charAt(result.length() - 1) > digit) {\n                result.deleteCharAt(result.length() - 1);\n                k--;\n            }\n\n            result.append(digit);\n        }\n\n        result.delete(result.length() - k, result.length());\n\n        int nonZeroIndex = 0;\n        while (nonZeroIndex < result.length() && result.charAt(nonZeroIndex) == \'0\') {\n            nonZeroIndex++;\n        }\n\n        return (nonZeroIndex == result.length()) ? "0" : result.substring(nonZeroIndex);\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        int n = num.length();\n\n        if (k >= n) {\n            return "0";\n        }\n\n        vector<char> result;\n\n        for (int i = 0; i < n; i++) {\n            char digit = num[i];\n\n            while (k > 0 && !result.empty() && result.back() > digit) {\n                result.pop_back();\n                k--;\n            }\n\n            result.push_back(digit);\n        }\n        result.resize(result.size() - k);\n\n        string result_str(result.begin(), result.end());\n        size_t nonZeroIndex = result_str.find_first_not_of(\'0\');\n\n        return (nonZeroIndex == string::npos) ? "0" : result_str.substr(nonZeroIndex);\n    }\n};\n```\n\n\n# Complexity\n- Time complexity: O(N) {Approx}\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n
['Python', 'C++', 'Java', 'Python3']