Video explanation ✅ || How to 🤔 this type of problems in Interview 🔥 || Monotonic Stack 📈
If you like the solution Please Upvote and subscribe to my youtube channel\n# Intuition\n Describe your first thoughts on how to solve this problem. \nDry Run f
***If you like the solution Please Upvote and subscribe to my youtube channel***\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDry Run few examples and observe how you can choose local minima at every step\n\n\n# ***Easy Video Explanation***\n\nhttps://youtu.be/HEIgLwwQNDQ\n    \n\n# Code\n\n\n```C++ []\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        // Stack to store the digits\n        stack<char> numStack;\n        \n        // Iterating through each digit in the number\n        for (char digit : num) {\n            // While there are remaining removals (k) and the stack is not empty and the current digit is smaller than the top of the stack\n            while (k > 0 && !numStack.empty() && digit < numStack.top()) {\n                // Remove digits from the stack\n                numStack.pop();\n                k--;\n            }\n            // Push the current digit to the stack\n            numStack.push(digit);\n        }\n        \n        // After iterating through the number, if there are still remaining removals, remove digits from the stack\n        while (k > 0 && !numStack.empty()) {\n            numStack.pop();\n            k--;\n        }\n\n        // Construct the result string by popping digits from the stack\n        string temp = "";\n        while (!numStack.empty()) {\n            temp.push_back(numStack.top());\n            numStack.pop();\n        }\n        // Reverse the result string to get the correct order\n        reverse(temp.begin(), temp.end());\n\n        // Remove leading zeros and construct the final result\n        int m = temp.size();\n        string result = "";\n        int foundNonZero = 0;\n        for (int i = 0; i < m; i++) {\n            if (temp[i] == \'0\' && foundNonZero == 0) {\n                continue;\n            } else {\n                result.push_back(temp[i]);\n                foundNonZero = 1;\n            }\n        }\n        // If the result is empty, return "0"\n        if (result.size() == 0)\n            result.push_back(\'0\');\n        return result;\n    }\n};\n\n```\n```python []\nclass Solution:\n    def removeKdigits(self, num: str, k: int) -> str:\n        # List to store the digits\n        numStack = []\n        \n        # Iterating through each digit in the number\n        for digit in num:\n            # While there are remaining removals (k) and the stack is not empty and the current digit is smaller than the top of the stack\n            while k > 0 and numStack and digit < numStack[-1]:\n                # Remove digits from the stack\n                numStack.pop()\n                k -= 1\n            # Push the current digit to the stack\n            numStack.append(digit)\n        \n        # After iterating through the number, if there are still remaining removals, remove digits from the stack\n        while k > 0 and numStack:\n            numStack.pop()\n            k -= 1\n\n        # Construct the result string by popping digits from the stack\n        temp = ""\n        while numStack:\n            temp += numStack.pop()\n        # Reverse the result string to get the correct order\n        temp = temp[::-1]\n\n        # Remove leading zeros and construct the final result\n        result = ""\n        foundNonZero = 0\n        for digit in temp:\n            if digit == \'0\' and foundNonZero == 0:\n                continue\n            else:\n                result += digit\n                foundNonZero = 1\n        # If the result is empty, return "0"\n        if not result:\n            result = \'0\'\n        return result\n\n```\n```Java []\nimport java.util.Stack;\n\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        // Stack to store the digits\n        Stack<Character> numStack = new Stack<>();\n        \n        // Iterating through each digit in the number\n        for (char digit : num.toCharArray()) {\n            // While there are remaining removals (k) and the stack is not empty and the current digit is smaller than the top of the stack\n            while (k > 0 && !numStack.isEmpty() && digit < numStack.peek()) {\n                // Remove digits from the stack\n                numStack.pop();\n                k--;\n            }\n            // Push the current digit to the stack\n            numStack.push(digit);\n        }\n        \n        // After iterating through the number, if there are still remaining removals, remove digits from the stack\n        while (k > 0 && !numStack.isEmpty()) {\n            numStack.pop();\n            k--;\n        }\n\n        // Construct the result string by popping digits from the stack\n        StringBuilder temp = new StringBuilder();\n        while (!numStack.isEmpty()) {\n            temp.append(numStack.pop());\n        }\n        // Reverse the result string to get the correct order\n        temp.reverse();\n\n        // Remove leading zeros and construct the final result\n        int m = temp.length();\n        StringBuilder result = new StringBuilder();\n        int foundNonZero = 0;\n        for (int i = 0; i < m; i++) {\n            if (temp.charAt(i) == \'0\' && foundNonZero == 0) {\n                continue;\n            } else {\n                result.append(temp.charAt(i));\n                foundNonZero = 1;\n            }\n        }\n        // If the result is empty, return "0"\n        if (result.length() == 0)\n            result.append(\'0\');\n        return result.toString();\n    }\n}\n\n```\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N)\n    \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N)\n\n# Less Number of lines (By tackling leading zero\'s in the begining)\n\n\n```C++ []\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        // Stack to store the digits\n        stack<char> numStack;\n        \n        // Iterating through each digit in the number\n        for (char digit : num) {\n            // While there are remaining removals (k) and the stack is not empty and the current digit is smaller than the top of the stack\n            while (k > 0 && !numStack.empty() && digit < numStack.top()) {\n                // Remove digits from the stack\n                numStack.pop();\n                k--;\n            }\n            if(numStack.empty() && digit==\'0\')\n                continue;\n            // Push the current digit to the stack\n            numStack.push(digit);\n        }\n        \n        // After iterating through the number, if there are still remaining removals, remove digits from the stack\n        while (k > 0 && !numStack.empty()) {\n            numStack.pop();\n            k--;\n        }\n\n        // Construct the result string by popping digits from the stack\n        string temp = "";\n        while (!numStack.empty()) {\n            temp.push_back(numStack.top());\n            numStack.pop();\n        }\n        // Reverse the result string to get the correct order\n        reverse(temp.begin(), temp.end());\n\n        // If the result is empty, return "0"\n        if (temp.size() == 0)\n            temp.push_back(\'0\');\n        return temp;\n    }\n};\n```\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n\n*Thank you* \uD83D\uDE00
['Stack', 'Monotonic Stack', 'Python', 'C++', 'Java']