Simple C++ solution using stack. Explained
Approach is simple. We need a number which is minimum, thus we need to remove the most significant digits first. For eg. if we have a number having digits 1-4 t
Approach is simple. We need a number which is minimum, thus we need to remove the most significant digits first. For eg. if we have a number having digits 1-4 then 1234 would be the minimum and not 2314 or anything else. So in case of 2314, we remove 3 first, and then we go for 2 (Because they are more significant than 4). Observing this simple idea, we need to remove any digit which is greater than its following digit. Thats why we deleted 3 as it was greater than 1 and similiarly 2 as it was also greater than 1.\n\nIn order to accomplish this, we use stack Data Structure where we pop the top if it is greater than current digit.\n\nThe conditions mentioned in while loop are important to avoid any Runtime Error. For eg. ["10001" 2] the answer is "0" but if we don\'t mention the condition !s.empty(), then the while loop will run on empty stack and try to pop the top which doesn\'t exist thus throwing RE.  \n\n```\nclass Solution {\npublic:\n    string removeKdigits(string str, int k) {\n        if(str.length() <= k)\n            return "0";\n        if(k == 0)\n            return str;\n        \n        stack <char> s;\n        \n        s.push(str[0]);\n        \n        for(int i = 1; i<str.length(); i++){\n            while(k>0 && !s.empty() && str[i]<s.top()){\n                k--;\n                s.pop();\n            }\n            s.push(str[i]);\n            \n\t\t\t// We pop any preceeding zeroes\n            if(s.size() == 1 && str[i] == \'0\')\n                s.pop();\n        }\n        \n\t\t// For cases like "12345" where every str[i] > s.top()\n        while(k && !s.empty()){\n            k--;\n            s.pop();\n        }\n        string res = "";\n        while(!s.empty()){\n            res.push_back(s.top());\n            s.pop();\n        }\n        \n        reverse(res.begin(), res.end());\n        if(res.size() == 0)\n            return "0";\n        else\n        return res;\n    }\n};\n```
['Stack', 'Greedy', 'C']