Simple C++ Solution of 8 ms without space by checking pairs with Explanation
Here, we have k as number of digits to be removed. So, each time we remove the digits we just subtract k by 1.\nAlso, We need to remove the beginning zeroes, an
Here, we have k as number of digits to be removed. So, each time we remove the digits we just subtract k by 1.\nAlso, We need to remove the beginning zeroes, and before returning result check if the result is empty or not. \nNote:- This is without using any extra space. \nFor Example:- num = "219897511" and k = 4\n**2** 1 9 8 9 7 5 1 1                 here 2 > 1 so erase it and reduce k = 3\n=> **1** 9 8 9 7 5 1 1              here 1 < 9 so skip it and increment i and k = 3\n=> 1 **9** 8 9 7 5 1 1              here 9 > 8 so erase it and reduce k = 2\n=> 1 **8** 9 7 5 1 1                 here 8 < 9 so skip it and increment i and k = 2\n=> 1 8 **9** 7 5 1 1                here 9 > 7 so erase it and reduce k = 1\n=> 1 **8** 7 5 1 1                   here 8 > 7 so erase it and reduce k = 0\n=> Result:-  1 7 5 1 1\n\n```\nclass Solution {\npublic:\n\n//Main Logic is compare 2 adjacent elements, 2 cases:-\n//1)  If the first element is less than or equal then increment i\n//2) if first element is greater then remove that element\n//Note:- edigits between pipes are into consideration. \n//       4 4 4 | 5  3|\n//here 5 and 3 are compared so delete 5 now the point is that 4 is also greater than 3 so if k is still greater than 0 then compare 4 and 3 and delete 4 that\'s why set i-- after deletion\n\n\n\t//Function to remove leading zeroes if any\n    void removeLeadingZeroes(string &num) {\n        while(num.length() > 0 && (num.compare(0, 1, "0") == 0)) {              //simple checking the first digit in num, until it is not 0 remove first digit\n            num.erase(num.begin()+0);\n        }\n    }\n\t//function to remove from the end of num if after iterating, k is still left\n    void removeLeftOver(string &num, int k) {\n        int i;\n        i = num.length() - k;\n        num.erase(num.begin()+i, num.end());\n    }\n    \n    string removeKdigits(string num, int k) {\n        int i = 0;\n        while((i < num.length()-1) && (k>0)) {                  //iterating until we have not traversed string completely or k > 0\n\t\t//if num = "12" then num[i] = 1 and num[i+1] = 2 so if k = 1 then check 1 and 2 if 1 is less then 1 than don\'t remove it \n            if(num[i] <= num[i+1])                            \n                i++;\n\t\t\t//if num = "21" then num[i] = 2 and num[i+1] = 1 so if k = 1 then check 1 and 2 if 2 is greater than 1 then  remove it \n            else\n            {\n                num.erase(num.begin()+i);\n                k--;\n                if(i > 0)                                 //if i != 0 then the previous element before the deleted element may or may not be greater than next one so decrement i to compare them\n                    i--;\n            }\n        }\n\t\t//if k is left then remove the left over from left as an example "222222"\n\t\t//in this case above loop will not erase any digit so this condition will remove from end k digits\n        if(k!=0)\n            removeLeftOver(num, k);\n\t\t//remove trailing zeroes\n        if(num.compare(0,1,"0") == 0)          //string compare function having arguments starting index, number of characters and other string i.e. str1.compare(starting_index, number_of_characters, str2)\n            removeTrailingZeroes(num);\n        if(num == "")\n            return "0";\n        return num;\n    }
['String', 'C', 'C++']