-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCS_53_SubsequencesOfAString.cpp
More file actions
40 lines (38 loc) · 919 Bytes
/
CS_53_SubsequencesOfAString.cpp
File metadata and controls
40 lines (38 loc) · 919 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// tc = O(2^n * n), sc = O(2^n)
vector<string> subsequences(string str)
{
int n = str.length();
vector<string> ans;
// for n = 3, (1<<n) i.e. 2^3 will be 8
for (int num = 1; num < (1 << n); num++)
{
// use num to create subseqences
string temp = "";
// num -> set bit -> index -> char -> include
for (int i = 0; i < n; i++)
{
// i -> index
char ch = str[i];
// if there's a set bit at i idx in the num then include the char at that
// index
int mask = (1 << i);
if (num & mask)
{
temp.push_back(ch);
}
}
ans.push_back(temp);
}
for (auto s : ans)
cout << s << endl;
return ans;
}
int main()
{
string str;
cin >> str;
subsequences(str);
return 0;
}