Removing character that occurs after and are greater

#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
int k;
cin>>s>>k;
stackst;
for(int i=0;i<s.length();i++){
if(st.empty()){
st.push(s[i]);
}else{
char top = st.top();
if(s[i]-‘0’<top-‘0’){
while(k>0 && !st.empty() && s[i]-‘0’<top-‘0’){
st.pop();
k–;
if(!st.empty()){
top=st.top();
}
}
}
st.push(s[i]);
}
}
while(k>0){
st.pop();
k–;
}
string ans="" ;
while(!st.empty()){
ans += st.top();
st.pop();
}
reverse(ans.begin(),ans.end());
cout<<ans;
return 0;
}

I was not removing the trailing zeros from the answer it is working fine now.

1 Like