-
-
Save SAgarwal-24/b4c34ac08a6b12a9ebc4d8252553524d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| string removeKdigits(string num, int k) { | |
| ios_base::sync_with_stdio(false); | |
| cin.tie(NULL); | |
| int n = num.size(); | |
| stack<char> mystack; | |
| //Store the final string in stack | |
| for(char c: num) | |
| { | |
| while(!mystack.empty() && k>0 && mystack.top()>c) | |
| { mystack.pop(); k-=1; } | |
| if(!mystack.empty() || c!='0') | |
| mystack.push(c); | |
| } | |
| //Now remove the largest values from the top of the stack | |
| while(!mystack.empty() && k--) | |
| mystack.pop(); | |
| if(mystack.empty()) | |
| return "0"; | |
| //Now retrieve the number from stack into a string (reusing num) | |
| while(!mystack.empty()) | |
| { | |
| num[n-1] = mystack.top(); | |
| mystack.pop(); | |
| n-=1; | |
| } | |
| return num.substr(n); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment