Simple Explanation with code | C++
Approach :\n1. Our aim is to get the minimum possible number, after removing any k digits from the given string.\n2. By observation and intuition, we see that f
**Approach :**\n1. Our aim is to get the minimum possible number, after removing any k digits from the given string.\n2. By observation and intuition, we see that for achieving our aim, we need to make the string as much *non-decreasing* as possible.\n\nLet me explain, point 2 with an example - \n\n![image](https://assets.leetcode.com/users/images/65c3bb68-ac28-4eeb-b2f8-b09ca95c5fe6_1613621751.0444.png)\n\n3. From above observaion, we infer that we need to remove the first k peaks that we encounter from the left side.\nHere, s[i] is considered as a peak if, s[i] > s[i+1].\n\n4.  A special case may occur, where there is no peak. (in such a case the string is arranged in a non-decreasing order.)\n\nExample - Input = "1234"...... here, there is no peak.\nSo, we just remove k digits from the right side, to obtain the output.\n\n5. The following code, will make the steps more clear.\n\n\n```\nclass Solution {\npublic:\n    string removeKdigits(string s, int k)\n    {\n        stack<char> stk; // stack declared\n        \n        for(auto c : s) //iterating over the input string\n        {\n            while(!stk.empty() && k>0 && stk.top()>c) // remove peaks from left side, k times\n            {\n                k--;\n                stk.pop();\n            }\n            \n            if(c!=\'0\' || !stk.empty()) \n                stk.push(c); // put the current char into stack, only if it\'s non 0....put 0 only if stack is not empty, as putting 0 as the leftmost digit of a number is of no use.\n        }\n        \n        while(k>0 && !stk.empty()) // special case handling (step 4)\n        {\n            stk.pop();\n            k--;\n        }\n        \n        string ans = "";\n        while(stk.size()) // collecting the answer from stack \n        {\n            ans += stk.top();\n            stk.pop();\n        }\n        \n        reverse(ans.begin(), ans.end());\n        return ans=="" ? "0" : ans; \n    }\n};\n```\n\nUpvote if it\'s helpful.\nGet more solutions [HERE](https://github.com/noob-hu-yaar/Leetcode/tree/master/nandincpp).\n\n****
[]