1
// ��������Please upvote if it helps ��������4
string removeKdigits(string num, int k) {5
// number of operation greater than length we return an empty string6
if (num.length() <= k) return "0";8
// k is 0 , no need of removing / preforming any operation9
if (k == 0) return num;11
string res = ""; // result string12
stack<char> s; // char stack14
s.push(num[0]); // pushing first character into stack16
for (int i = 1; i < num.length(); ++i) {17
while (k > 0 && !s.empty() && num[i] < s.top()) {18
// if k greater than 0 and our stack is not empty and the upcoming19
// digit, is less than the current top than we will pop the stack top26
// popping preceding zeroes27
if (s.size() == 1 && num[i] == '0') s.pop();30
while (k && !s.empty()) {31
// for cases like "456" where every num[i] > num.top()37
res.push_back(s.top()); // pushing stack top to string38
s.pop(); // pop the top element41
reverse(res.begin(), res.end()); // reverse the string43
if (res.length() == 0) return "0";