**Remove K digits (Leetcode 402)

Problem Link: https://leetcode.com/problems/remove-k-digits/

class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        
        s=[]
        n=len(num)
        c=0
        
        if(k>=n):
            return "0"
        
        if(k==0):
            return num
        
        s=[]
        ans=""
        # Monotonous stack
        # Remove the large numbers present on the most significant bits side
        for i in range(n):
            if(len(s)==0):
                s.append(num[i])
            else:
                if(int(s[-1])<=int(num[i])):
                    s.append(num[i])
                else:
                    while(c<k and len(s)>0 and int(s[-1])>int(num[i])):
                        c+=1
                        s.pop()
                    s.append(num[i])
        
        # If you can remove more elements, remove them from last
        if(c<k):
            s=s[:len(s)-(k-c)]
            
        for i in range(len(s)):
            if(s[i]!='0'):
                s=s[i:]
                break 
        else:
            # This means the stack consists only 0s        
            return "0"
            
        ans=''
        for i in s:
            ans=ans+i
        return ans      

Last updated