#include
#include
using namespace std;
void replace(string&s,int i,int j,int k)
{
if(i==s.length())
{
return;
}
if(k==s.length())
{
return;
}
if(s[i]==‘x’)
{
swap(s[i],s[j]);
swap(s[i],s[k]);
replace(s,i+1,j-1,k+1);
}
else
{
replace(s,i+1,j,k+1);
}
}
int main() {
string s;
cin>>s;
int j=s.length()-1;
int k=1;
replace(s,0,j,k);
cout<<s<<endl;
return 0;
}
What is wrong with this code
hello @Kunalgoyal
your code will not maintain relative ordering( by relative ordering i mean if a comes before b then in output also they should maintain same order).
We follow a recursive approach to solve this problem. At each instance , we check whether the first character is a ‘x’ or not. We recursively obtain the result for the rest of the string i.e. the substring from index 1. If the first character of our current string is an ‘x’ , we concatenate it to the end of our resultant string. Else , we simply place concatenate it back to the front of our resultant string. The second part of the string is the result we obtained recursively.
refer this -> MOVE ALL X TO END
what is the use of s.substr
substr is function that return substring.
in code s.substr(1) will return substring starting from index 1 of string s.
for example string is axbxc.
so it starts from xbxc in substr.
yeah right . . . . . . . .
okay. plz explain me code of cb numbers. i am unable to understand it.
please raise a separate doubt for the same (just like u raised for this )
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.