Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.

&nbsp;
Example 1:

Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.


Example 2:

Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.


Example 3:

Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.


&nbsp;
Constraints:


	1 &lt;= k &lt;= num.length &lt;= 105
	num consists of only digits.
	num does not have any leading zeros except for the zero itself.


# Lets make monotonically growing stack and save the indexes of popped elements into deletes dict.
#as soon as len(delete) == k delete those indexes from the initial string and thats the answer.
#if len(delete) < k remove k-len(delete) chars from right and thats the answer
class Solution:
    def removeKdigits(self, s: str, k: int) -> str:
        if len(s) == k:
            return '0'
        stack = []
        delete = {}
        for i in range(len(s)):

            while stack and s[i] < stack[-1][0]:
                delete[stack.pop()[1]] = 1
                if len(delete) == k:
                    break
            if len(delete) == k:
                return self.deleteindexes(s, delete, k)
            stack.append([s[i], i])
        s1 = self.deleteindexes(s, delete, k)

        return str(int(s1[:len(s1)-k +len(delete)]))


    def deleteindexes(self, s, delete, k):
        if not delete:
            return s
        if len(delete) == k:
            return str(int(''.join([c for ind, c in enumerate(s) if ind not in delete])))
        else:
            return ''.join([c for ind, c in enumerate(s) if ind not in delete])
class Solution {
    public String removeKdigits(String num, int k) {
        int n = num.length();
        if(n == k){
            return "0";
        }
        Deque<Character> dq = new ArrayDeque<>();
        for(char ch : num.toCharArray()){
            while(!dq.isEmpty() && k > 0 && dq.peekLast() > ch){
                dq.pollLast();
                k--;
            }
            dq.addLast(ch);
        }
        StringBuilder sb = new StringBuilder();
        while(!dq.isEmpty() && dq.peekFirst() == '0'){
            dq.pollFirst();
        }
        while(!dq.isEmpty()){
            sb.append(dq.pollFirst());
        }
        if(k >= sb.length()){
            return "0";
        }
        return sb.length() == 0 ? "0" : sb.toString().substring(0,sb.length()-k);
    }
}
								// 😉😉😉😉Please upvote if it helps 😉😉😉😉
class Solution {
public:
    string removeKdigits(string num, int k) {
        // number of operation greater than length we return an empty string
        if(num.length() <= k)   
            return "0";
        
        // k is 0 , no need of removing /  preforming any operation
        if(k == 0)
            return num;
        
        string res = "";// result string
        stack <char> s; // char stack
        
        s.push(num[0]); // pushing first character into stack
        
        for(int i = 1; i<num.length(); ++i)
        {
            while(k > 0 && !s.empty() && num[i] < s.top())
            {
                // if k greater than 0 and our stack is not empty and the upcoming digit,
                // is less than the current top than we will pop the stack top
                --k;
                s.pop();
            }
            
            s.push(num[i]);
            
            // popping preceding zeroes
            if(s.size() == 1 && num[i] == '0')
                s.pop();
        }
        
        while(k && !s.empty())
        {
            // for cases like "456" where every num[i] > num.top()
            --k;
            s.pop();
        }
        
        while(!s.empty())
        {
            res.push_back(s.top()); // pushing stack top to string
            s.pop(); // pop the top element
        }
        
        reverse(res.begin(),res.end()); // reverse the string 
        
        if(res.length() == 0)
            return "0";
        
        return res;
        
        
    }
};
var removeKdigits = function(num, k) {
    if(k == num.length) {
        return '0'
    }
    let stack = []
    for(let i = 0; i < num.length; i++) {
        while(stack.length > 0 && num[i] < stack[stack.length - 1] && k > 0) {
            stack.pop()
            k--
        }
        stack.push(num[i])
    }
    while(k > 0) {
        stack.pop()
        k--
    }
    while(stack[0] == 0 && stack.length > 1) {
        stack.shift()
    }
    return stack.join('')
}
Remove K Digits