2 Easy Interview Approaches ✅ || without stack 🔥|| with stack ✅ || Beat 99.59%💯 || Simple Approach
\n\n# Approach 1\n Describe your approach to solving the problem. \nusing vector as a stack (Stack Approach)\n\n# Complexity\n- Time complexity: O(n)\n Add your
![Runtime complexity2.png](https://assets.leetcode.com/users/images/b8ee3f6a-cc2d-489a-9e53-15f158970778_1712805876.9058824.png)\n\n# Approach 1\n<!-- Describe your approach to solving the problem. -->\nusing vector as a stack (Stack Approach)\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  vector stack take O(n) space in worst case\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        vector<char>stk;\n        string ans;\n        for(char c :num){\n            while(k>0 && !stk.empty() && stk.back()>c){\n                stk.pop_back();\n                k--;\n            }\n            stk.push_back(c);\n        }\n        while(k>0){\n            stk.pop_back();\n            k--;\n        }\n        for(char c : stk){\n            if(ans.empty() && c==\'0\') continue;\n            ans.push_back(c);\n\n        }\n        return ans.empty()?"0":ans;\n    }\n};\n```\n```Java []\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        Stack<Character> stk = new Stack<>();\n        StringBuilder ans = new StringBuilder();\n        for (char c : num.toCharArray()) {\n            while (k > 0 && !stk.isEmpty() && stk.peek() > c) {\n                stk.pop();\n                k--;\n            }\n            stk.push(c);\n        }\n        while (k > 0) {\n            stk.pop();\n            k--;\n        }\n        for (char c : stk) {\n            if (ans.length() == 0 && c == \'0\') continue;\n            ans.append(c);\n        }\n        return ans.length() == 0 ? "0" : ans.toString();\n    }\n}\n```\n```python []\nclass Solution(object):\n    def removeKdigits(self, num, k):\n        stk = []\n        ans = ""\n        for c in num:\n            while k > 0 and stk and stk[-1] > c:\n                stk.pop()\n                k -= 1\n            stk.append(c)\n        while k > 0:\n            stk.pop()\n            k -= 1\n        for c in stk:\n            if not ans and c == \'0\':\n                continue\n            ans += c\n        return ans if ans else \'0\'\n\n```\n# Approach 2\n<!-- Describe your approach to solving the problem. -->\nwithout Stack\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n  only returning string take space. we have to mandatory return it so it can not be counted as extra space  \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    string removeKdigits(string num, int k) {\n        string s = ""; s += num[0];\n        for(int i=1;i<num.length();++i) {\n            // if removal attempts are left (k>0)\n            // and nums[i] is less than last element of s\n            // then remove last element of s and come back to nums[i] again\n            if(k > 0 && s.length() > 0 && num[i] < s[s.length()-1]) {\n                s.pop_back();\n                k--;\n                i--;\n                continue;\n            }\n            else\n            s += num[i];\n        }\n        // if removal attempts are left remove characters at the end\n        // because at this point s contains characters in ascending order\n        // so preferable to remove larger characters at the end\n        while(k > 0 && s.length() > 0) {\n            s.pop_back();\n            k--;\n        }\n\n        while(s[0] == \'0\') {      // remove trailing zeroes if any\n            s.erase(s.begin());\n        }\n        \n        if(s.length() == 0) s = "0";   // if final s is empty then indicates 0\n        return s;\n    }\n};\n```\n```Java []\nclass Solution {\n    public String removeKdigits(String num, int k) {\n        StringBuilder s = new StringBuilder();\n        s.append(num.charAt(0));\n        for (int i = 1; i < num.length(); ++i) {\n            if (k > 0 && s.length() > 0 && num.charAt(i) < s.charAt(s.length() - 1)) {\n                s.deleteCharAt(s.length() - 1);\n                k--;\n                i--;\n                continue;\n            }\n            s.append(num.charAt(i));\n        }\n        while (k > 0 && s.length() > 0) {\n            s.deleteCharAt(s.length() - 1);\n            k--;\n        }\n        while (s.length() > 0 && s.charAt(0) == \'0\') {\n            s.deleteCharAt(0);\n        }\n        if (s.length() == 0) {\n            s.append(\'0\');\n        }\n        return s.toString();\n    }\n}\n\n```\n```python []\nclass Solution(object):\n    def removeKdigits(self, num, k):\n        s = num[0]\n        for i in range(1, len(num)):\n            if k > 0 and len(s) > 0 and num[i] < s[-1]:\n                s = s[:-1]\n                k -= 1\n                continue\n            s += num[i]\n        while k > 0 and len(s) > 0:\n            s = s[:-1]\n            k -= 1\n        while s and s[0] == \'0\':\n            s = s[1:]\n        if not s:\n            s = \'0\'\n        return s\n\n\n```\n![upvote cat3.png](https://assets.leetcode.com/users/images/c33e5d62-fb75-49c4-9f6e-def709f80862_1712805898.959742.png)\n\n\n
['String', 'Stack', 'Swift', 'C', 'Python', 'C++', 'Java', 'Kotlin', 'JavaScript']