(33). 16.3 7 Best Problem on Recursion. - anishsingh90/Data_Structure_And_Algorithm_In_Cpp_github.io GitHub Wiki

//Reverse a string using Recursion #include <bits/stdc++.h> using namespace std;

void reverse(string s){ if(s.length() == 0){ //best case return; }

string ros = s.substr(1);
reverse(ros);
cout << s[0];

}

int main(){

reverse("binod");

return 0;

}

/* OUTPUT: donib */

// Replace 'pi' with '3.14' in string;. #include <bits/stdc++.h> using namespace std;

void replacePi(string s){ if(s.length() == 0){ //base case return; }

if(s[0]=='p' && s[1]=='i'){
	cout << "3.14";
	replacePi(s.substr(2));
}
else{
	cout << s[0];
	replacePi(s.substr(1));
}	

}

int main(){

replacePi("pippppiiiipi");

return 0;

} /* OUTPUT: 3.14ppp3.14iii3.14 */

//Tower of Hanoi. #include <bits/stdc++.h> using namespace std;

void towerofHanoi(int n, char src, char dest, char helper){ if(n==0){ return; //best case }

towerofHanoi(n-1,src,helper,dest);
cout<< "Move from " << src << " to " << dest << endl;
towerofHanoi(n-1,helper,dest,src);

}

int main(){ towerofHanoi(3,'A','C','B'); return 0; }

/* OUTPUT: Move from A to C Move from A to B Move from C to B Move from A to C Move from B to A Move from B to C Move from A to C */

//Remove all duplicates from the string #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("aaaabbbeeecdddd") << endl; return 0; }

/* OUTPUT: abecd */

//Move all x to the end of the string. #include <bits/stdc++.h> using namespace std;

string moveallx(string s){ if(s.length() == 0){ return ""; } char ch = s[0]; string ans = moveallx(s.substr(1));

if(ch=='x'){
	return ans+ch;
}
return ch+ans;

}

int main(){

cout << moveallx("axxbdxcefxhix") << endl;

return 0;

}

/* OUTPUT: abdcefhixxxxx */

//Generate all substring of a string. #include <bits/stdc++.h> using namespace std;

void subseq(string s, string ans){ if(s.length()==0){ cout << ans << endl; return; }

char ch = s[0];
string ros = s.substr(1);

subseq(ros,ans);
subseq(ros,ans+ch);

}

int main(){ subseq("ABC",""); cout <<" ";

return 0;

}

/* OUTPUT: String of a string C B BC A AC AB ABC */

//Generate substring with ASCII number. #include <bits/stdc++.h> using namespace std;

void subseq(string s, string ans){ if(s.length()==0){ cout << ans << endl; return; } char ch = s[0]; int code = ch; string ros = s.substr(1);

subseq(ros,ans);
subseq(ros,ans+ch);
subseq(ros,ans+to_string(code));  //to_string is used for covert into string

}

int main(){

subseq("AB","");

return 0;

}

//this code is run in online compiler /* OUTPUT: B 66 A AB A66 65 65B 6566 */

//Print all possible words from phone digits. #include <bits/stdc++.h> using namespace std;

string keypadArr[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

void keypad(string s, string ans){ if(s.length()==0){ cout << ans << endl; return; }

char ch = s[0];
string code = keypadArr[ch-'0'];
string ros = s.substr(1);

for(int i=0; i<code.length(); i++){
	keypad(ros, ans + code[i]);
}

}

int main(){ keypad("23",""); return 0; }

/* OUTPUT: ad ae af bd be bf cd ce cf */