Brute-Force to Optimal Solution || C++ (Don't jump to optimal solution directly)
Note : Don\'t Jump into Optimal Solution directly without checking Brute-Force Solution\n\n1) Brute Force Solution\n\nTime complexity : O(NK)\nSpace complexity 
**Note : Don\'t Jump into Optimal Solution directly without checking Brute-Force Solution**\n\n**1) Brute Force Solution**\n\nTime complexity : O(N*K)\nSpace complexity : O(1)\n\n```\nclass Solution {\n    private:\n    string removeKdigit(string num, int k)\n    {\n        // base case\n        if(k == 0)\n            return num;\n        \n        int i = 0;\n        \n        // find index in which cur ele > next ele\n        for(i=0; i < num.size() - 1; i++)\n        {\n            int cur = num[i] - \'0\';\n            int next = num[i+1] - \'0\';\n            \n            if(cur > next) \n                break;\n        }\n        \n        if(num.size() > 0)\n            num.erase(num.begin() + i);\n        \n        // check if any leading zeros in num\n        while(num.size() > 1 && num[0] == \'0\')\n            num.erase(num.begin());\n        \n        // if num length is zero\n        if(num.size() == 0)\n            num = "0";\n        \n        // recursion for next iteration\n        return removeKdigit(num, k-1);\n    }\n    \npublic:\n    string removeKdigits(string num, int k) {\n        \n        return removeKdigit(num, k);\n    }\n};\n```\n\n**2) Optimal Solution**\nTime Complexity : O(N)\nSpace complexity : O(N)\n```\nclass Solution {\n    private:\n    string removeKdigit(string num, int k)\n    {\n        string res = "";\n        \n        for(char &ch : num)\n        {\n            // maintain res string with increasing order\n            while(res.size() > 0 && k && res.back() > ch)\n            {\n                res.pop_back();\n                k--;\n            }\n            \n            // we should not add any zeros if res length is zero\n            // because, it may lead to add zeros at the front that doesn\'t make sense       \n            if(res.size() > 0 || ch != \'0\')\n                res.push_back(ch);\n        }\n        \n        // check if any k digits needs to remove\n        while(res.size() && k)\n        {\n            res.pop_back();\n                k--;\n        }\n        \n        if(res.size() == 0) return "0";\n        \n        return res;\n    }\n    \npublic:\n    string removeKdigits(string num, int k) {\n        \n        return removeKdigit(num, k);\n    }\n};\n```
['C', 'Iterator']