-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recursion_3.cpp
118 lines (116 loc) · 2.36 KB
/
Recursion_3.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
#include <iostream>
using namespace std;
void reverse(string s)
{
// Time Complexity : O(n^2)
// Space Complexity : O(n^2)
if (s.length() == 0)
return;
reverse(s.substr(1));
cout << s[0];
}
void replacePi(string s)
{
// Time Complexity : O(n^2)
// Space Complexity : O(n^2)
if (s.length() == 0)
return;
if (s[0] == 'p' && s[1] == 'i')
{
cout << "3.14";
replacePi(s.substr(2));
}
else
{
cout << s[0];
replacePi(s.substr(1));
}
}
string removeDup(string s)
{
// Time Complexity : O(n^2)
// Space Complexity : O(n^2)
if (s.length() == 0)
return "";
char c = s[0];
string ans = removeDup(s.substr(1));
if (c == ans[0])
return ans;
else
{
return c + ans;
}
}
string moveallXs(string s)
{
// Time Complexity : O(n^2)
// Space Complexity : O(n^2)
if (s.length() == 0)
return "";
char c = s[0];
string ans = moveallXs(s.substr(1));
if (c == 'x')
return ans + c;
else
{
return c + ans;
}
}
void subseq(string s, string ans = "")
{
// Time Complexity : O(2^n)
// Space Complexity : O(2^n)
if (s.length() == 0)
{
cout << ans << endl;
return;
}
char c = s[0];
string ros = s.substr(1);
subseq(ros, ans);
subseq(ros, ans + c);
}
void subseq2(string s, string ans = "")
{
// Time Complexity : O(2^n)
// Space Complexity : O(2^n)
if (s.length() == 0)
{
cout << ans << endl;
return;
}
char c = s[0];
int code = c;
string ros = s.substr(1);
subseq2(ros, ans);
subseq2(ros, ans + c);
subseq2(ros, ans + to_string(code));
}
string keypadArr[] = {"", ".", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void keypad(string s, string ans = "")
{
// Time Complexity : O(2^n)
// Space Complexity : O(2^n)
if (s.length() == 0)
{
cout << ans << endl;
return;
}
char c = s[0];
string code = keypadArr[c - '0'];
string ros = s.substr(1);
for (int i = 0; i < code.length(); i++)
{
keypad(ros, ans + code[i]);
}
}
int main()
{
reverse("Hailee");
replacePi("pippxpixxpish");
cout << removeDup("aaabbbdddcccdegggh");
cout << moveallXs("xxvchdjxxbdjkxxyewu");
subseq2("AB");
keypad("23");
return 0;
}