[BEATS 92%] EASY STACK & QUEUE SOLUTION
\n\n# \uD83D\uDC93**_PLEASE CONSIDER UPVOTING_**\uD83D\uDC93\n\n \n\n\n# Intuition\nMy first thought was to maintain a structure that allows for efficient addit
<div align="center">\n\n# \uD83D\uDC93**_PLEASE CONSIDER UPVOTING_**\uD83D\uDC93\n\n</div>\n\n***\n# Intuition\nMy first thought was to maintain a structure that allows for efficient addition and removal of digits while keeping track of the smallest possible number as we process the input string. The goal is to remove `k` digits from the number to form the smallest possible result while ensuring we preserve the relative order of the remaining digits.\n\n\u2B50 **Stack Solution**:\n\n![image.png](https://assets.leetcode.com/users/images/497322ba-6ba4-4c19-a082-1cba97ab42b8_1728481474.4696548.png)\n\n\u2B50 **Deque Solution**:\n\n![image.png](https://assets.leetcode.com/users/images/1bb2a228-bbbd-42cc-bd96-2836d1bebfc3_1728481499.8021934.png)\n\n\n# Approach\n\n### \uD83D\uDD26 Deque-Based Solution\n1. **Use a Deque:** A deque (double-ended queue) is chosen for its ability to efficiently add and remove elements from both ends.\n2. **Iterate Over Digits:** For each digit in the input number, compare it with the last digit in the deque.\n3. **Pop Larger Digits:** If the current digit is smaller than the last digit in the deque and we still have digits to remove (`k > 0`), pop the last digit from the deque.\n4. **Add Current Digit:** Push the current digit onto the deque.\n5. **Final Adjustment:** After processing all digits, if there are any remaining digits to remove (`k > 0`), remove them from the end of the deque.\n6. **Build the Result:** Construct the final number from the deque, ensuring to handle leading zeros.\n\n### \uD83D\uDD26 Stack-Based Solution\n1. **Use a Stack:** A stack is used to maintain the digits in the desired order.\n2. **Iterate Over Digits:** As each digit is processed, compare it with the top of the stack.\n3. **Pop Larger Digits:** If the current digit is smaller than the top of the stack and we can still remove digits, pop from the stack.\n4. **Push Current Digit:** Push the current digit onto the stack.\n5. **Final Adjustment:** If `k` digits remain to be removed, continue popping from the stack.\n6. **Build the Result:** Construct the final number by popping all elements from the stack and reversing them.\n\n# Complexity\n- **Time Complexity:** Both solutions run in $$O(n)$$ time, where $$n$$ is the length of the input string, as each digit is processed a limited number of times (pushed and popped).\n  \n- **Space Complexity:** \n  - The deque-based solution has a space complexity of $$O(n)$$ for storing the digits.\n  - The stack-based solution also has a space complexity of $$O(n)$$ due to the stack usage.\n\n# Code\n\n### \uD83D\uDCA1Deque-Based Solution\n```java\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        if (num.length() == k) return "0";\n\n        Deque<Character> deque = new LinkedList<>();\n\n        for (char ch : num.toCharArray()) {\n            while (!deque.isEmpty() && k > 0 && deque.peekLast() > ch) {\n                deque.removeLast();\n                k--;\n            }\n            deque.addLast(ch);\n        }\n\n        while (k > 0) {\n            deque.removeLast();\n            k--;\n        }\n\n        StringBuilder ans = new StringBuilder();\n        while (!deque.isEmpty()) {\n            ans.append(deque.removeFirst());\n        }\n\n        while (ans.length() > 1 && ans.charAt(0) == \'0\') {\n            ans.deleteCharAt(0);\n        }\n\n        return ans.isEmpty() ? "0" : ans.toString();\n    }\n}\n```\n\n### \uD83D\uDCA1Stack-Based Solution\n```java\nimport java.util.Stack;\n\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        if (num.length() == k) return "0";\n\n        Stack<Character> stack = new Stack<>();\n        int i = 0;\n\n        while (i < num.length()) {\n            while (!stack.isEmpty() && k > 0 && ((stack.peek() - \'0\') > (num.charAt(i) - \'0\'))) {\n                stack.pop();\n                k--;\n            }\n            stack.push(num.charAt(i));\n            i++;\n        }\n\n        while (k > 0) {\n            stack.pop();\n            k--;\n        }\n\n        StringBuilder ans = new StringBuilder();\n\n        while (!stack.isEmpty()) {\n            ans.append(stack.pop());\n        }\n\n        ans.reverse();\n\n        while (ans.length() > 1 && ans.charAt(0) == \'0\') {\n            ans.deleteCharAt(0);\n        }\n\n        return ans.isEmpty() ? "0" : ans.toString();\n    }\n}\n```
['String', 'Stack', 'Greedy', 'Queue', 'Monotonic Stack', 'Java']