Easiest W/Explanation 🔥✅Python3 // C++ // C // Java  // Python ✅✔️ Beats 100% 😎
Intuition\n\n\n\n\nC++ []\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        string ans = "";\n        for (char& c : num) {\n  
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/9f1b4393-a1b5-42f9-868c-8d60c5a63f33_1712799068.1077394.jpeg)\n![Screenshot 2024-04-11 071741.png](https://assets.leetcode.com/users/images/a330e113-4658-45e0-97b5-47f3e5e37930_1712800097.4758646.png)\n\n![Screenshot 2024-04-11 070132.png](https://assets.leetcode.com/users/images/92831d7a-1dc1-4eb7-842e-aabacd028145_1712799108.26533.png)\n```C++ []\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        string ans = "";\n        for (char& c : num) {\n            while (ans.size() && ans.back() > c && k) {\n                ans.pop_back();\n                k--;\n            }\n            if (ans.size() || c != \'0\')\n                ans.push_back(c);\n        }\n\n        while (ans.size() && k--)\n            ans.pop_back();\n        return (ans == "") ? "0" : ans;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def removeKdigits(self, num: str, k: int) -> str:\n        stack = []\n        for n in num:\n            while( stack and int(stack[-1]) > int(n) and k):\n                stack.pop()\n                k -= 1\n            stack.append(str(n))\n\n        while(k):\n            stack.pop()\n            k -= 1\n            \n        i = 0\n        while( i <len(stack) and stack[i] == "0" ):\n            i += 1\n            \n        return \'\'.join(stack[i:]) if (len(stack[i:]) > 0) else "0"   \n```\n```C []\nvoid push(char);\nchar pop(void);\nchar end_peek(void);\nbool is_stack_empty(void);\nint stack_size(void);\nint top;\nchar* stack;\nvoid push(char c) { stack[++top] = c; }\n\nchar pop(void) { return stack[top--]; }\n\nbool is_stack_empty(void) { return top < 0 ? true : false; }\n\nchar end_peek(void) { return stack[top]; }\n\nint stack_size(void) { return top + 1; }\nchar* removeKdigits(char* num, int k) {\n    if (strlen(num) <= k)\n        return "0";\n    if (k == 0)\n        return num;\n\n    top = -1;\n    stack = calloc(strlen(num), sizeof(char));\n    for (int i = 0; i < strlen(num); ++i) {\n        // compare the current value and the previous local peak point(the last\n        // element of the stack). if num[i] is smaller than the local peak\n        // point, pop element E.g., num = "12842479", k= 6 push 1, 2, 8, and now\n        // the current value is 4 128 4, 4 < 8, so pop 8 -> 12 4\n        while (!is_stack_empty() && num[i] < end_peek() && k > 0) {\n            pop();\n            --k;\n        }\n        push(num[i]);\n    }\n\n    // concatenate the characters in stack\n    char* s = calloc(strlen(num) + 1, sizeof(char));\n    memcpy(s, stack, sizeof(char) * stack_size());\n\n    // remove trailing digits\n    // Now, the num = "122479", k = 4\n    // it\'s an non-descending order, the k is still greater than 0\n    // remove the digits from the end(the bigger one)\n    // the result is 12 (2479 is removed)\n    while (k-- > 0 && s)\n        s[strlen(s) - 1] = \'\\0\';\n\n    // remove leading zeros\n    while (s[0] == \'0\' && strlen(s) > 1)\n        s++;\n    free(stack);\n    return s;\n}\n```\n```Java []\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        int top = -1, n = num.length(), st = 0, digits = num.length() - k;\n        if (n == k)\n            return "0";\n\n        char[] arr = num.toCharArray();\n        for (int i = 0; i < n; i++) {\n            while (top >= 0 && arr[top] > arr[i] && k > 0) {\n                top--;\n                k--;\n            }\n            top++;\n            arr[top] = arr[i];\n        }\n\n        while (st <= top && arr[st] == \'0\')\n            st++;\n        String s = new String(arr);\n        return (st >= digits) ? "0" : s.substring(st, digits);\n    }\n}\n```\n```python []\nclass Solution(object):\n    def removeKdigits(self, num, k):\n        """\n        :type num: str\n        :type k: int\n        :rtype: str\n        """\n        if k >= len(num):\n            return "0"  \n\n        stack = []\n        removed = 0\n        \n        for digit in num:\n            while stack and stack[-1] > digit and removed < k:\n                stack.pop()\n                removed += 1\n            \n            stack.append(digit)\n        \n        while removed < k:\n            stack.pop()\n            removed += 1\n        \n \n        result = \'\'.join(stack).lstrip(\'0\')\n        return result if result else "0"  \n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n) -- string length\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n# Code\n```\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        int top = -1, n = num.length(), st = 0, digits = num.length() - k;\n        if (n == k)\n            return "0";\n\n        char[] arr = num.toCharArray();\n        for (int i = 0; i < n; i++) {\n            while (top >= 0 && arr[top] > arr[i] && k > 0) {\n                top--;\n                k--;\n            }\n            top++;\n            arr[top] = arr[i];\n        }\n\n        while (st <= top && arr[st] == \'0\')\n            st++;\n        String s = new String(arr);\n        return (st >= digits) ? "0" : s.substring(st, digits);\n    }\n}\n```\n# Please upvote if it\'s useful for you .. your support motivates me to do regular postings my best efforts\n# ThankYou.....\n![th.jpeg](https://assets.leetcode.com/users/images/4b1612df-5695-4253-8d39-0fc4cd7e4769_1712799304.3455062.jpeg)\n
['String', 'Stack', 'Greedy', 'C', 'Monotonic Stack', 'Python', 'C++', 'Java', 'Python3']