My C++ O(n) solution
\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        int n = num.size(), remain = n - k;\n        if(remain == 0) return "0";\n  
```\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        int n = num.size(), remain = n - k;\n        if(remain == 0) return "0";\n        string s = "";\n        for(auto x: num){\n            while(n > remain && !s.empty() && s.back() - '0' > x - '0'){\n                s.pop_back();\n                n--;\n            }\n            s.push_back(x);\n        }\n        int i = 0;\n        while(i < s.size() && s[i] == '0') i++;\n        return s.substr(i, remain) == "" ? "0" : s.substr(i, remain);\n    }\n};\n```
['C++']