Clear Java Solution Using Deque
public class Solution {\n     \n     // the goal is to create an ascending sequence\n    public String removeKdigits(String num, int k) {\n        if (num == nu
public class Solution {\n     \n     // the goal is to create an ascending sequence\n    public String removeKdigits(String num, int k) {\n        if (num == null || num.length() == 0) return "0";\n        Deque<Character> nums = new LinkedList<>();\n        char[] digits = num.toCharArray();\n        for (int i = 0; i < digits.length; i++) {\n            while (!nums.isEmpty() && k > 0 && digits[i] < nums.peekLast()) {\n                nums.pollLast();\n                k--;\n            }\n            nums.offer(digits[i]);\n        }\n        \n        while (k > 0) {\n            nums.pollLast();\n            k--;\n        }\n        \n        while (!nums.isEmpty() && nums.peek() == '0') {\n            nums.poll();\n        }\n        \n        StringBuilder sb = new StringBuilder();\n        while (!nums.isEmpty()) {\n            sb.append(nums.poll());\n        }\n        \n        return sb.length() == 0 ? "0" : sb.toString();\n    }\n}
[]