-
Notifications
You must be signed in to change notification settings - Fork 127
/
Recurrsion.cpp
52 lines (41 loc) · 1 KB
/
Recurrsion.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
#include <bits/stdc++.h>
using namespace std;
// The main recursive method
// to print all possible
// strings of length k
void printAllKLengthRec(char set[], string prefix,
int n, int k)
{
// Base case: k is 0,
// print prefix
if (k == 0)
{
cout << (prefix) << endl;
return;
}
for (int i = 0; i < n; i++)
{
string newPrefix;
// Next character of input added
newPrefix = prefix + set[i];
// k is decreased, because
// we have added a new character
printAllKLengthRec(set, newPrefix, n, k - 1);
}
}
void printAllKLength(char set[], int k,int n)
{
printAllKLengthRec(set, "", n, k);
}
// Driver Code
int main()
{
cout << "First Test" << endl;
char set1[] = {'a', 'b'};
int k = 3;
printAllKLength(set1, k, 2);
cout << "Second Test\n";
char set2[] = {'a', 'b', 'c', 'd'};
k = 1;
printAllKLength(set2, k, 4);
}