-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetAllSubsequences.cpp
More file actions
40 lines (33 loc) · 1.12 KB
/
getAllSubsequences.cpp
File metadata and controls
40 lines (33 loc) · 1.12 KB
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 <iostream>
#include <vector>
using namespace std;
// A subsequence of a string is a new string
// that is formed from the original string by
// deleting some (can be none) of the characters without
// disturbing the relative positions of the remaining characters
void dfs(const std::string &str, int idx, const std::string path,
std::vector<std::string> &subsequences);
std::vector<std::string> getAllSubsequences(const std::string &str) {
string path;
vector<string> subsequences;
dfs(str, 0, path, subsequences);
return subsequences;
}
void dfs(const std::string &str, int idx, const std::string path,
std::vector<std::string> &subsequences) {
if (idx == str.length()) {
subsequences.push_back(path);
return;
}
dfs(str, idx + 1, path + str[idx], subsequences);
dfs(str, idx + 1, path, subsequences);
}
int main() {
string input = "abc";
vector<string> result = getAllSubsequences(input);
cout << "All subsequences of '" << input << "':" << endl;
for (const string &subsequence : result) {
cout << subsequence << endl;
}
return 0;
}