C++ 6ms 10 lines solution with comments
\nstring removeKdigits(string num, int k) {\n       string ans = "";                                         // treat ans as a stack in below for loop\n       \
```\nstring removeKdigits(string num, int k) {\n       string ans = "";                                         // treat ans as a stack in below for loop\n       \n       for (char c : num) {\n           while (ans.length() && ans.back() > c && k) {\n               ans.pop_back();                                  // make sure digits in ans are in ascending order\n               k--;                                             // remove one char\n           }\n           \n           if (ans.length() || c != '0') { ans.push_back(c); }  // can't have leading '0'\n       }\n       \n       while (ans.length() && k--) { ans.pop_back(); }          // make sure remove k digits in total\n       \n       return ans.empty() ? "0" : ans;\n}\n```
[]