Back to course home
0% completed
Vote For New Content
My solution
Mohammed Dh Abbas
Jul 31, 2024
class Solution: def removeKdigits(self, num: str, k: int) -> str: count = 0 stack = [] for d in num: # while the number is smaller that the top of stack keep popping while count < k and len(stack) > 0 and int(d) < int(stack[-1]): stack.pop() count += 1 # don't insert 0 if the stack is empty otherwise insert if len(stack) == 0: if int(d) > 0: stack.append(d) else: stack.append(d) # remaining number that needs to be popped out to meet k while len(stack) > 0 and count < k: count += 1 stack.pop() result = "".join(stack) # empty string means zero return result if result != "" else "0"
0
0
Comments
Comments