Python | Easy
see the Successfully Accepted Submission\n\n# Code\n\nclass Solution:\n    def removeKdigits(self, num: str, k: int) -> str:\n        stack = []\n        # Iter
[see the Successfully Accepted Submission](https://leetcode.com/problems/remove-k-digits/submissions/1229162669/?envType=daily-question&envId=2024-04-11)\n\n# Code\n```\nclass Solution:\n    def removeKdigits(self, num: str, k: int) -> str:\n        stack = []\n        # Iterate through each digit in num\n        for digit in num:\n            # While there are digits in the stack, k > 0, and the current digit is smaller than the top of the stack\n            while k > 0 and stack and stack[-1] > digit:\n                # Remove the top of the stack\n                stack.pop()\n                k -= 1\n            # Append the current digit to the stack\n            stack.append(digit)\n        \n        # If there are still digits left to remove, remove them from the end of the stack\n        while k > 0:\n            stack.pop()\n            k -= 1\n        \n        # Remove leading zeros\n        while stack and stack[0] == \'0\':\n            stack.pop(0)\n        \n        # If stack is empty, return \'0\', otherwise, return the concatenated stack as a string\n        return \'\'.join(stack) if stack else \'0\'\n        \n```\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']