Remove k digits

August 27, 2022

stack

Problem URL: Remove k digits

We will use a monotonic decreasing stack to keep track of the digits. We will remove most significant k digits from the stack and then return the values, if the stack is empty at that point we will return 0. While pushing any values in the stack we will also ingore 0, as we can just ignore leading 0's.

class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        stack = []
        for n in num:
            while stack and k and stack[-1] > n:
                stack.pop()
                k -= 1
            if stack or n != '0':
                stack.append(n)

        if k:
            stack = stack[:-k]

        return "".join(stack) or "0"

Time Complexity: O(n)
Space Complexity: O(n)