JAVA Bruteforce -> Memoization -> Greedy
Bruteforce [ TLE ]\n\nclass Solution {\n    String res = "";\n    public void helper(String num, String curr, int index, int k){\n        if(k==0){\n           
### Bruteforce [ TLE ]\n```\nclass Solution {\n    String res = "";\n    public void helper(String num, String curr, int index, int k){\n        if(k==0){\n            curr+=num.substring(index);\n            double res_ = res.length()!=0?Double.parseDouble(res):Double.MAX_VALUE;\n            double curr_ = curr.length()!=0?Double.parseDouble(curr):0;\n            if(curr_ < res_){\n                res = curr;\n            }\n            return;\n        }\n        if(index >= num.length())\n            return;\n        \n        // 2 options at each index\n        \n        // consider it\n        // not consider it\n    \n        char c  = num.charAt(index);\n        helper(num, curr+c, index+1,  k);\n        helper(num, curr, index+1, k-1);\n    }\n    public String removeKdigits(String num, int k) {\n        if(k==num.length())\n\t\t\treturn "0";\n        helper(num, "", 0, k);\n        \n        // check for trailing zeros\n        int i = 0;\n        for(;i<res.length();i++){\n            if(res.charAt(i)!=\'0\')\n                break;\n        }\n        res = res.substring(i);\n        return res.length()==0?"0":res;\n    }\n}\n```\n### Caching or Memoization Faster than Bruteforce [ Still TLE :( ]\n```\nclass Solution {\n    String res = "";\n    public String recur(String num, int index, int k, String[][] memo){\n        if(k==0)\n            return num.substring(index);\n        if(index>=num.length())\n            return "";\n        if(!memo[index][k].equals("-1"))\n            return memo[index][k];\n        char c = num.charAt(index);\n        String one  =  c  + recur(num, index+1,  k, memo);\n        String second = recur(num, index+1, k-1, memo);\n        double one_ =  Double.parseDouble(one);\n        double second_ = second.length()!=0?Double.parseDouble(second):0;\n        if(second_ <  one_)\n            memo[index][k] = second;\n        else\n            memo[index][k] = one;\n        return memo[index][k];\n    }\n    public String removeKdigits(String num, int k) {\n        if(k==num.length())\n            return "0";\n        String[][] memo = new String[num.length()+1][k+1];\n        for(String[] mem: memo){\n            Arrays.fill(mem, "-1");\n        }\n        res = recur(num, 0, k, memo);\n        // helper(num, "", 0, k);\n        // check for trailing zeros\n        int i = 0;\n        for(;i<res.length();i++){\n            if(res.charAt(i)!=\'0\')\n                break;\n        }\n        res = res.substring(i);\n        return res.length()==0?"0":res;\n    }\n}\n```\n### Greedy Approach using Monotonic Stack\n**Time : O(n)\nSpace : O(n)**\n```\n class Solution {\n    public String removeKdigits(String num, int k) {\n        int n = num.length();\n        LinkedList<Integer> stack = new LinkedList<>();\n        for(int i=0;i<n;i++){\n            int curr = num.charAt(i)-\'0\';\n            while(k > 0 && stack.size() > 0 && stack.peekLast().intValue() > curr){\n                stack.pollLast();\n                k--;\n            }\n            stack.addLast(curr);\n        }\n        // if k is still available remove from last \n        // can be case where we dont delet eany of the characters\n        // example: 1 2 3 4 5, in such case we\'ll not pop anything from stack\n        for(int i=0;i<k;i++){\n            stack.pollLast();\n        }\n        StringBuilder sb = new StringBuilder();\n        for(int item: stack){\n            if(item==0 && sb.length()==0){ // incase first element is \'0\'\n                continue;\n            } \n            sb.append(item);\n        }\n        if(sb.length()==0){\n            return "0";\n        }\n        return sb.toString();\n    }\n}\n```
['Dynamic Programming', 'Stack', 'Greedy', 'Memoization', 'Java']