[C++] With explanation || Memory Usage less than 100.00% of other submissions ||O(1) space
Approach:\n\nI will start with an example.\n\nfor ex.\n\nnum = 142532 k = 1\n\nAs k = 1, we have to remove one digit.\nNow, we can remove any digit from num but
Approach:\n\nI will start with an example.\n\nfor ex.\n\nnum = 142532 k = 1\n\nAs k = 1, we have to remove one digit.\nNow, we can remove any digit from num but the remaining number should be smallest.\n\nIn any number the left digits are more significant digits compared to right digits. So we will try to remove the digits from left.\nWe cannot  start removing the digits from left directly as removing 1 from left gives us 42532 which is certainly not the smallest possible number.\n\nIf we remove the leftmost digit and the digit next to it is larger than the removed digit then the number we get will not be the smallest\nHere leftmost is 1 and 4 is next to it.\n\nBut if the number next to it had been smaller, then we had definitely got the smallest number.\nfor ex. instead of 142532 if num was 642532, removing 6 would have given us the smallest number.\n\nhence, our approach will be,\n\n1. Start from left\n2. If the next digit is smaller than current remove current digit\n\nNow suppose we didn\'t find any number smaller than current, that means the digits are sorted\nEx.  num = 2456789 \nIn this if we want to remove digits, we have to remove the rightmost digit.\n\n\n```\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        \n        if(num.size()<=k)\n            return "0";\n        string ret = num;\n        int del = k;\n\t\tfor (int j = 0; j < k; j++)\n\t\t{\n\t\t\tfor (int i = 0; i < ret.size() - 1; i++)\n\t\t\t{\n\t\t\t\tif (ret[i] > ret[i + 1])   //check if current digit is greater than next\n\t\t\t\t{\n\t\t\t\t\tret.erase(i, 1);    // if true remove that digit\n\t\t\t\t\tdel--;          // update count of digits to be removed\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (del >= 1)  // there are still some digits need to removed - remove rightmost digits\n\t\t{\n            int index = ret.size()-del;\n\t\t\tret.erase(index,del);\n\t\t}\n        ret.erase(0, ret.find_first_not_of(\'0\'));  // removed leading 0\'s\n\t\tif(ret.size()==0)\n            return "0";\n        \n\t\treturn ret;\n    }\n};\n```\n
[]