Help with error

code: https://ide.codingblocks.com/s/467660

problem: https://leetcode.com/problems/remove-k-digits/

test case: num = 10200, k = 1 is failing

hello @raghav007

please explain ur logic.
not getting from ur code

I’m maintaining an increasing stack whenever the top element of stack is greater I’m popping till the top is smaller and also reducing k there is also a check if for example num is 123 then i removing k elements after this process is complete I convert the elements of stack into a string and I have also created a check for leading 0’s

here after erasing the character from ans the idexing will change.
so cant do i++ everytime.

check this->


another way to avoid leading zero is to insert a char in stack only if ur stack in not empty

   return ans.empty() ? "0" : ans;

please explain this

see we are never pushing leading zeros.
so for case like this -> 00000

our ans (string/stack) will remain empty hence we are checking it explicitly and returing 0

if (ans.length() || c != '0') { ans.push_back(c);
what is the purpose of ans.length()

when can we push the new char in the stack?
a) if we already have some non zero entries in stack. that why checking ans.length()(if greater than 0 )
b) if the current character is non zero . that is why c!=0
if any of them is true we can push

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.