#include<bits/stdc++.h>
using namespace std;
void replacei(string & s,string & t,int i=0){
t=t+s[i];
if(i==s.length()-1)return ;
if(s[i]!=s[i+1])return replacei(s,t,i+1);
return replacei(s,t,i+2);
}
int main(){
string s,t="";cin>>s;
replacei(s,t);
cout<<t;
return 0;
}
Why this approach is not working in recursively removing duplicates from string
Hey @argus
check for
aaaa ,it wont work
#include<bits/stdc++.h>
using namespace std;
void replacei(string & s,string & t,int i=0){
t=t+s[i];
if(i==s.length()-1)return ;
if(s[i]!=s[i+1])return replacei(s,t,i+1);
t.pop_back();//dup character we pushed in t
return replacei(s,t,i+1);//i+1 here
}
int main(){
string s,t="";cin>>s;
replacei(s,t);
cout<<t;
return 0;
}
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.
t.po_back not working in code blocks
Please share the screenshot of the error