Short 10 lines O(n) Java Code
Basically remove those digits start descending\nO(n) Time O(n) space\n\n\npublic class Solution {\n    public static String removeKdigits(String num, int k) {\n
Basically remove those digits start descending\nO(n) Time O(n) space\n\n```\npublic class Solution {\n    public static String removeKdigits(String num, int k) {\n        StringBuilder sb = new StringBuilder();\n        for(char c : num.toCharArray()) {\n            while(k > 0 && sb.length() != 0 && sb.charAt(sb.length() - 1) > c) {\n                sb.setLength(sb.length() - 1);\n                k--;\n            }\n            if(sb.length() != 0 || c != '0') sb.append(c);  // Only append when it is not leading zero\n        }\n        if(k >= sb.length()) return "0";\n        sb.setLength(sb.length() - k);  // use all remaining k\n        return sb.toString();  \n    }\n}\n```
[]