Remove k Digits - Easiest Explanation you'll see 👩‍💻👨‍💻
\n# Approach\n Describe your approach to solving the problem. \nThe approach to solving this question is very easy. We have been given an integer k which denote
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach to solving this question is very easy. We have been given an integer `k` which denotes the times we can remove a digit from the number to make it the smallest posisble number.\n\nWe will use a stack to store the digits that are smaller than the current digit in the number. If it is greater than the current number , we keep popping keeping in mind that `k must be greater than 0` and the digit in the stack should be always less than the currnet digit in the number .\nGo through the **Code WalkThrough** section to have a clearer idea.\n\n---\n# Code WalkThrough\n\n**Step 1**\nWe start with finding the length of the number that is given to us as a String. \nThen , we put a condition that if `k==len` the code will return a "0" because that will be invalid of course!\nSince we will be using a stack , we initialise it with type Integer.\n\nRelevant part of the code is :\n```\n int len=num.length();\n\n       if(k==len)\n       {\n        return "0";\n       }\n     \n     Stack<Character>stack = new Stack<>();\n```\n\n**Step 2**\n \nNow , we are ready to iterate through the number . While `k>0` and stack is not empty , we check if the topmost element in stack is greater than the current digit in the string. \n\nIf it is , then that means , we can remove it from stack since we want to keep the smallest digit possible .\nAfter popping , we need to decrement k by 1 , since k acts as a limit of how many digits can be removed.\n\nOtherwise , we just push the digit in the stack for future comparision.\n\nRelevant part of code :\n\n```\nfor(int i=0;i<len;i++)\n     {\n     while(k>0 &&!stack.isEmpty() && stack.peek()>num.charAt(i))\n     {\n        stack.pop();\n        k--;\n     }\n\n     stack.push(num.charAt(i));\n```\n**Step 3**\n\n\nAfter this , we use another loop .\n\nIf k were greater than 0, this loop would pop elements from the stack until k becomes 0, ensuring that exactly k digits are removed if not already done during the main loop. \n\n\nThen , we need to build a final string using StringBuilder to form our smallest number . We pop the digits from the stack and append them in the `result` string.\n\nnow , since the stack supports LIFO ie last element to get in the stack is the first to get out ,when we were processing the number , the last digit we encountered must have gone to the top of the stack but we want the last digit in the last position only in the real and final number .Hence , we need to reverse the order of the `result` string .\n\nRelevant part of the code :\n```\nwhile(k>0)\n{\nstack.pop();\n    k--;\n  }\n\nStringBuilder result = new StringBuilder();\n\nwhile(!stack.isEmpty())\n  {\n  result.append(stack.pop());\n\n  }\n\n\n  result.reverse();\n```\n\n**Step 4**\n\nLastly , we need to check for any leading zeroes in the final numebr formed and remove them because we know that 009 is same as 9 right? \n\nFinally , we return the result string .\n\nRelevant part of the code :\n\n```\n while(result.length()>1 && result.charAt(0)==\'0\')\n  {\n    result.deleteCharAt(0);\n  }\n\n  return result.toString();\n\n}\n}\n```\n\n\n\n---\n\n# Complexity\n- Time complexity : $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n# Code\n```java []\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        \n       int len=num.length();\n\n       if(k==len)\n       {\n        return "0";\n       }\n     \n     Stack<Character>stack = new Stack<>();\n\n     for(int i=0;i<len;i++)\n     {\n     while(k>0 &&!stack.isEmpty() && stack.peek()>num.charAt(i))\n     {\n        stack.pop();\n        k--;\n     }\n\n     stack.push(num.charAt(i));\n     \n\n    }\n\n  while(k>0)\n  {\n    stack.pop();\n    k--;\n  }\n\n\n\n  StringBuilder result = new StringBuilder();\n\n  while(!stack.isEmpty())\n  {\n      result.append(stack.pop());\n\n  }\n\n\n  result.reverse();\n\n\n  while(result.length()>1 && result.charAt(0)==\'0\')\n  {\n    result.deleteCharAt(0);\n  }\n\n  return result.toString();\n\n}\n}\n```\n\n---\n\n\n**Feel free to discuss this problem in the comments and do upvote if this explanation helped you !**\n\n\n**Happy Coding**
['String', 'Stack', 'Monotonic Stack', 'Java']