-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprint-all-subset-of-given-string.cpp
55 lines (46 loc) · 1.07 KB
/
print-all-subset-of-given-string.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
/**
* @file print-all-subset-of-given-string.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-08-10
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
using namespace std;
void backtracking(string &str,int n,int k,string &candidate,bool choosen[]){
if(k==n){
cout<< "{ ";
for(int i = 0; i < n; ++i) {
if(choosen[i]){
cout << candidate[i];
}
}
cout<< " } ";
cout << endl;
return;
}
if(k>n) return;
int nc = 2;
bool c[nc];
c[0] = true;
c[1] = false;
for(int i = 0; i < nc; i++) {
candidate[k] = str[k];
choosen[k] = c[i];
backtracking(str,n,k+1,candidate,choosen);
}
}
void print_all_subset_of_given_string(string str){
int n = str.size();
string candidate = new char[n];
bool choosen[n] = {false};
backtracking(str,n,0,candidate,choosen);
}
int main(int argc, const char** argv) {
string S= "abc";
print_all_subset_of_given_string(S);
return 0;
}