forked from 7harshit20/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion_problem.cpp
131 lines (119 loc) · 2.86 KB
/
recursion_problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
string keypadArr[]={"","./","abc","def","ghi"};
vector<vector<int>> ans;
string reverse(string s,string ans){
if(s.length()==0){
return ans;
}
ans+=s[s.length()-1];
s.erase(s.length()-1,1);
return reverse(s,ans);
}
void replace_pi(string s){
if(s.length()==0)return;
if(s[0]=='p' && s[1]=='i'){
cout<<"3.14";
s.erase(0,2);
replace_pi(s);
}else{
cout<<s[0];
s.erase(0,1);
replace_pi(s);
}
}
void tower_of_hanoi(int n,char A,char B,char C){
if(n==0)return;
tower_of_hanoi(n-1,A,C,B);
cout<<"move from "<<A<<" to "<<C<<endl;
tower_of_hanoi(n-1,B,A,C);
}
string remove_duplicate(string s, string ans){
if(s.length()==0)return ans;
if(s[0]!=ans[ans.length()-1])ans+=s[0];
return remove_duplicate(s.substr(1),ans);
}
string x_to_end(string s){
if(s.length()==0)return "";
char ch=s[0];
string ans = x_to_end(s.substr(1));
if(ch=='x'){
return ans+=ch;
}
return ch+ans;
}
void subseq(string s,string ans){
if(s.length()==0){
cout<<ans<<endl;
return;
}
subseq(s.substr(1),ans);
subseq(s.substr(1),s[0]+ans);
}
void subseqascii(string s,string ans){
if(s.length()==0){
cout<<ans<<endl;
return;
}
int asc=s[0];
subseqascii(s.substr(1),ans);
subseqascii(s.substr(1),ans+s[0]);
subseqascii(s.substr(1),ans+to_string(asc));
}
void keypad(string s, string ans){
if(s.length()==0){
cout<<ans<<endl;
return;
}
for(int i=0;i<keypadArr[s[0]-'0'].length();i++){
keypad(s.substr(1),ans+(keypadArr[s[0]-'0'])[i]);
}
}
// void phone(string s, string ans){
// if(s.length()==0){
// cout<<ans<<endl;
// return;
// }
// for(int i=0;i<keypadArr[s[0]-'0'].length();i++){
// string temp=keypadArr[s[0]-'0'];
// ans+=temp[i];
// phone(s.substr(1),ans);
// }
// }
void permuteArr(vector<int> arr,int index){
if(index==arr.size()){
ans.push_back(arr);
return;
}
for(int i=index;i<arr.size();i++){
swap(arr[index],arr[i]);
permuteArr(arr,index+1);
swap(arr[index],arr[i]);
}
}
int main(){
// string s="";
// cout<<reverse("harshit",s)<<endl;
// replace_pi("harshitpi");
// tower_of_hanoi(6,'A','B','C');
// cout<<remove_duplicate("aaaddbbbcceff","")<<endl;
// cout<<x_to_end("abxxjbssxxgdsxgx")<<endl;
// subseq("abc","");
// subseqascii("ABC","");
// keypad("234","");
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
permuteArr(v,0);
for(int i=0;i<ans.size();i++){
for(int j=0;j<v.size();j++){
cout<<ans[i][j]<<" ";
}cout<<endl;
}
return 0;
}