Given a string of characters, we need to remove all the duplicate characters in the given string.
This is tutorial file given by my professor.
This is working properly except for the test cases like:
Test case:
Expected output:
{a,b,a,c,d} --> {a,b,c,d}
Output Received:
{a,b,a,c,d} --> {a,b,a,c,d}
My code:
#include <bits/stdc++.h>
using namespace std;
string removeDup(string s){
if(s.length()==0){
return "";
}
char ch = s[0];
string ans = removeDup(s.substr(1));
if(ch==ans[0]){
return ans;
}
return (ch+ans);
}
int main(){
cout<<removeDup("abacd")<<endl;
return 0;
}
Please send the solution using recursion,if possible.